简体   繁体   中英

Can't get ajax post in php

I have been looking for an answer for my answer the entire day...I have an ajax post which sends a variable value to a php page. I think the ajax post works fine according to the success pop-up but i can't get the value on php...

Here is the sample the of ajax post:

var value = 150;
    /*$.post('index.php',{variable:essai});*/
    /*alert("Token:"+token);*/
    $.ajax({
        url:'index.php',
        type:'POST',
        data:'data1='+value,
        success : function(data)
        {
            alert("Succès envoi de la donnée!");
        },
        error : function(resultat,statut,erreur)
        {
            alert("Erreur d'envoi de la donnée!");
        },
        complete : function(resultat,statut){

        } 


    });

And for the php sample:

<div id="right-frame">
<?php include("get.php");
if (isset($_POST['data1']))
{
$variable = $_POST['data1'];
}
?>
<p class="text">Données de la borne :</p>
<table><tr>
<th bgcolor="9fbcdc"><p class="titre">Info 1</p></th>
<th bgcolor="9fbcdc"><p class="titre">Info 2</p></th>
<th bgcolor="9fbcdc"><p class="titre">Info 3</p></th>
<th bgcolor="9fbcdc"><p class="titre">Info 4</p></th>
</tr>
<tr>
<th><?php echo $variable; ?></th>
<th><?php echo $variable2; ?></th>
<th><?php echo $variable3; ?></th>
<th><?php echo $variable4; ?></th>
</tr></table>
</div>

Thank you

You're sending data as data:'data1='+value

Try sending it like this:

data: {
    'data1': value
}

replace

data:'data1='+value

with

data:{'data1':value}

From official documentation of jquery / ajax , it says data filed required key/value pair object.

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below)

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