drlq92444
drlq92444
2015-07-27 11:10

使用ajax / jquery从php获取变量

this code doesn't work for me ! i want to display the variable $nom in a notification using UIkit . here the php file (verif1.php) :

<?php



$IMEI = $_GET['IMEI'];
$region = $_GET['region'];
$numero = $_GET['numero'];
$nom = $_GET['nom_client'];

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

/* <input type="hidden" id="nom" value="valeurDeMariable"> </input> */

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO client ( nom_client,IMEI,numero ) VALUES ('$nom',$IMEI,$numero)";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully pour "."$nom";
    echo json_encode(array('nom' => $nom ));
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;


?>

` AND HERE's the javascript code :

<script language="Javascript">
$.ajax({
    url: 'verif1.php',
    type: 'POST',

    dataType: 'json',
    data: {
        "varA": nom
    },

    cache: false,
    success: function(data) {
        alert('Items added');
    },
    error: function(e) {
        console.log(e.message);
    }
});
</script>
<script language="Javascript">
$(document).ready(function() {

    setTimeout(function() {
        ui.notify('Notification 01', 'Cette premiere notification s\'affichera quelques secondes puis disparaitra.')
            .effect('slide');

        setTimeout(function() {
            ui.notify('Notification 02', 'Cette deuxieme notification s\'affichera plus longtemps que la premiere.')
                .closable()
                .hide(8000)
                .effect('slide');


        }, 200);
    }, 200);


});
</script>

the problem is that $.ajax function doesn't show anything and i don't know how to know if it's working or not!! please help :)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongtang6681 dongtang6681 6年前

    First thing:

    Replace all $_GET with $_POST

    Because, you have chose post method in: type: 'POST',

    $IMEI = $_GET['IMEI'];
    $region = $_GET['region'];
    $numero = $_GET['numero'];
    $nom = $_GET['nom_client'];
    

    To:

    $IMEI = $_POST['IMEI'];
    $region = $_POST['region'];
    $numero = $_POST['numero'];
    $nom = $_POST['nom_client'];
    

    Also, from the code you have provided:

    data: {"varA": nom},
    

    Where is nom defined?

    点赞 评论 复制链接分享
  • douhe4608 douhe4608 6年前

    i have a form

    <div id="content">
                <form method="get" action="verif1.php">
                    <b><label for="IMEI">Veuillez entrer votre IMEI : </label></b>
                    <input type="string" id="IMEI" name="IMEI"/><br /><br />
                    &nbsp;&nbsp;<b><label for="nom_client">Veuillez entrer votre nom : </label></b>
                    <input type="string" id="nom_client" name="nom_client"   /><br /><br />
                    <b><label for="region">Veuillez entrer votre région : </label></b>
                    <input type="string" id="region" name="region"   /><br /><br />
                    <b><label for="numero">Veuillez entrer votre numéro : </label></b>
                    <input type="string" id="numero" name="numero"   /><br /><br />
    
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="submit" value="Enter" />
                </form>
    
            </div>
    

    and from this form i go directly to verif1.php so idon't really know how to erase those $_GET

    点赞 评论 复制链接分享

相关推荐