简体   繁体   中英

get post data in a php page called by ajax

I have called the page search.php from ajax:

function search(){


                  var title=$("#search").val();

                  if(title!=""){

                     $.ajax({
                        type:"post",
                        url:"search.php",
                        data:"title="+title+"&idTratta="+<?php echo $idTratta ?>,
                        success:function(data){
                            $("#result").html(data);
                            $("#search").val("");
                         }
                      });
                  }



             }

Now in search.php I get the result of a query in a html table and I created radio buttons in a form that when each form is submitted I get the value of the radio button's clicked and update a row in the db:

$title = $_POST["title"];
$idTratta = $_POST["idTratta"];

$SimpleUsers = new SimpleUsers();
$users = $SimpleUsers -> searchUser($title, $idTratta);

foreach ($users as $user) :
    echo "<tr>
                    <td>" . $user["nome"] . "</td>
                    <td>" . $user["role"] . "</td>
                    <td class='right'><form action='' method='post'><label>
    <input type='radio' name=" . $user["nome"] . " id='c'  value='1' ";
    if ($user["role"] == 'configuratore')
        echo "checked='checked'  />C</label>";
    else
        echo "/>C</label>";
    echo "<label>
    <input type='radio' name=" . $user["nome"] . " id='va' value='2' ";
    if ($user["role"] == 'visualizzatore avanzato')
        echo "checked='checked'  />VA</label>";
    else
        echo "/>VA</label>";
    echo "<label>
    <input type='radio' name=" . $user["nome"] . " id='v' value='3' ";
    if ($user["role"] == 'visualizzatore')
        echo "checked='checked' />V </label>";
    else
        echo "/>V </label>";
    echo "<input type= 'submit' name='sub_" . $user["nome"] . "'value='Cambia'/></form></td>
                </tr>";

    $sub = 'sub_';

    if ($_POST[$sub . '' . $user["nome"]]) {

        $permission = $_POST[$user["nome"]];
        $SimpleUsers -> updateUserPermission($user["nome"], $idTratta, $permission);

    }
endforeach;

The problem is that in search.php I am unable to catch POST variables, how I can get it?

EDIT This is the piece of code which doesn't work:

if ($_POST[$sub . '' . $user["nome"]]) {

    $permission = $_POST[$user["nome"]];
    $SimpleUsers -> updateUserPermission($user["nome"], $idTratta, $permission);

}

The data shouldn't be parsed as text but instead as an js array like this:

data: {title: title, idTratta : <?php echo $idTratta ?>}, 

EDIT:

A example for sending the form data would be:

var formData = JSON.stringify($("#myForm").serializeArray());
formData['idTratta'] = <?php echo $idTratta ?>;
$.ajax({
     type:"post",
     url:"search.php",
     data:formData,
     success:function(data){
       $("#result").html(data);
       $("#search").val("");
    }
});

And than using json_decode in your php form to turn it into a PHP array again. Its kind of dirty but it should work

Try this method

$.post("`search.php`", { title : title , idTratta : '<?php echo $idTratta ?>'}, 
function (data) {
    $("`#result`").html(data);
    $("`#search`").val("");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM