drlq92444 2015-07-27 11:10
浏览 77
已采纳

使用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 2015-07-27 11:20
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型