Pass value of an input to PHP

1

The value is sent from a page to a modal of Bootstrap in the following way by means of the data-book-id="" of the label <a></a> .

<?php while($row = mysql_fetch_array($sql)){                                                                                                
        echo "    <tr>";
        echo " "?><td style="display:none;"><?php  
        echo $row['SecCarro']."</td>";           
        echo "    <td>".$row['CorreoCliente']."</td>";                                                  
        echo "    <td><a rel=\"tooltip\"title=\"Ver\" href=\"sys/detalleped.php\" data-toggle=\"modal\" data-target=\"#proModal\" data-book-id=\"".$row['SecCarro']."\" > <i class=\"fa fa-eye fa-lg\" ></i></a></td>";                                         
        echo "    </tr>"; }?> 

I capture it in the following way.

<script>
$('#proModal').on('show.bs.modal', function(e) {
var bookId = $(e.relatedTarget).data('book-id');
$(e.currentTarget).find('input[name="bookId"]').val(bookId);    
});</script>

<input type="text" name="bookId" id="bookId" value=""/>  
<?php
     $var =?? // Aqui necesito el valor del input... :/
?>

And here is where I stayed, I would appreciate any help possible.

    
asked by avargasma 03.05.2017 в 21:28
source

2 answers

3

First of all, remember that PHP is a language focused on the server side and JavaScript or JQuery is a language on the client side. Therefore what one can do is a "trick" to what one understands, by sending to the server from JQuery.

So you can implement AJAX for sending from the input to a php file in real time, making it seem to be running on the client side.

You can implement Ajax with Jquery in the following way:

CLIENT:

<input type="text" name="bookId" id="bookId" value=""/>  
<script>
  var dato = $('#bookId').val();
    $.ajax({
      data: {"dato" : dato},
      url: "archivoEnviarDato.php",
      type: "post",
      success:  function (response) {
        alert(response);
      }
    });
 </script>

SERVER:

<?php
    $var = $_POST['dato'];
    //Procedimiento a realizar
?>

I hope you understand that your situation is on the client side for what I see and therefore, as php lives on the server, you must send a message with the input you need.

This will help you not to leave your current page. In other words, you can continue to interact with the screen you are on.

Note:

If you require that the file php of a return value and JQuery take it, the simplest way is the following:

SERVER:

<?php
    $var = $_POST['dato'];
    echo $var.' de vuelta'; // concatenamos y devolvemos el valor a la pagina que requirió de esta solicitud
?>

CLIENT:

<input type="text" name="bookId" id="bookId" value=""/>  
<script>
  var dato = $('#bookId').val();
    $.ajax({
       data: {"dato" : dato},
       url: "archivoEnviarDato.php",
       type: "post",
    success:  function (response) {
       alert(response); //Arrojara un alert colocando el input + ' de vuelta'
    });
 </script>
  

If you are occupying a database, be careful with SQL injection and XSS attack

I hope it can be helpful. Greetings and success!

    
answered by 03.05.2017 в 21:50
2

The PHP code will only be executed if there is a client request, therefore you can do it using a <form> or through AJAX , but in this case I recommend using a <form> .

Remaining the code HTML :

<form action="tu_pagina.php" method="post">
     <input type="text" name="bookId" id="bookId" value=""/>
     <input type="submit" value="Enviar">
</form>

And the simple script in PHP :

<?php
$bookid = $_POST["bookId"]; //Ya tienes el valor del input en una variable PHP, ahora ya haz lo que desees con ella
?>
  

WARNING: If that variable is going to be transferred to a database   very careful with the SQL injection , the code that I showed you NO   avoid it .    Here you have good information on the theme .

I hope it helps you

    
answered by 03.05.2017 в 21:47