javascript - get a variable from php using ajax/jquery -
this code doesn't work me ! want display variable $nom in notification using uikit . here 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 pdo error mode exception $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $sql = "insert client ( nom_client,imei,numero ) values ('$nom',$imei,$numero)"; // use exec() because no results returned $conn->exec($sql); echo "new record created pour "."$nom"; echo json_encode(array('nom' => $nom )); } catch(pdoexception $e) { echo $sql . "<br>" . $e->getmessage(); } $conn = null; ?>
` , here's 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 $.ajax function doesn't show , don't know how know if it's working or not!! please :)
first thing:
replace $_get $_post
because, 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, code have provided:
data: {"vara": nom},
where nom
defined?
Comments
Post a Comment