How to send a data to javascript from php?


It turns out that I'm trying to pass a value of PHP to Javascript to validate me if there is already a certain amount of data.

The PHP script does its job if the data exists returns 1 and if it does not return 0 , the problem is that I do not know how to take that value again in Javascript .

I leave the code to see if anyone can help me.


!DOCTYPE html>
<html lang = "es">
    <meta charset = "UTF-8">
    <title>Formulario de registro</title>
    <link rel="stylesheet" href="../css/reg.css">
    <script src="../js/llenar.js"></script>
    <script src="../js/jquery-3.2.1.min.js"></script>
    <h1>Formulario de registro</h1>
    <form  method= "post" class = "form-registro">
        <h2> Crear capitanes</h2>
        <div class ="contenedor-input">
            <input type = "number" id = "cedula" name = "cedula" placeholder = "Cedula" class = "input-100" required>
            <input type = "text" id = "nombre" name = "nombre" placeholder = "Nombres" class = "input-48" required>
            <input type = "text" id = "apellido" name = "apellido" placeholder = "Apellidos" class = "input-48" required>
            <input type = "text" id = "user" name = "user" placeholder = "Usuario" class = "input-48" required>
            <input type = "text" id = "pwd" name = "pwd" placeholder = "Contraseña" class = "input-48" required>
            <select name = "congregacion" id = "cmbCongregacion" class = "input-100" required>
            <input type = "submit" value = " Registrar capitan" class = "btn-enviar" id = "btn-enviar">


window.onload = function(){
    var btnEnviar = document.getElementById("btn-enviar");
    btnEnviar.onclick = enviarForm;
    var cedula = document.getElementById("cedula");
    cedula.onblur = repetido;


function repetido(){
    var cedula = document.getElementById("cedula");
    var valor;
    valor = cedula.value;
    $.post("../php/repetido.php", {cedula: valor}, function(data){


asked by Familia Valencia Hdz 29.12.2017 в 17:20

3 answers


After the request is executed correctly, the data that the server returns arrives as a parameter to the function that is executed at the end of the request (success), in which case you have called the parameter data

$.post("../php/repetido.php", {cedula: valor}, function(data){
    $("#cedula").val(data); // para elementos de formulario (inputs, select, etc) no se usa '.html()' si no '.val()'

Why make an alert (data); I should show you that answer.

NOTE: It is important that PHP you are returning the value by means of a echo

answered by 29.12.2017 / 17:31

To see a PHP variable in Javascript, simply print it in the same javascript. I need you to explain better what exactly you want. Which variable do you want to see in javascript? Please edit your question or comment on my answer. Here the solution

<script type="text/javascript">
    var variablejs = '<?php echo $tuvariable;?>'
answered by 29.12.2017 в 17:29

Your problem arises because you are using the function html of jquery to fill the input, you must change it by val :

You have it this way:


It must be like this:

answered by 29.12.2017 в 17:31