how to test passing data from $.post to php file in query - php

I want to test passing data from $.post to php file in query
this is my $.post
$( "#get_route").on('click', function() {
var imei = $('#hisdeviceimei').val();
$.post( "vehicle_route_qry.php",{ imei:imei.replace(/,\s*$/, "") },function(data) {
alert(data);
});
});
this is php file
<?
include 'connect_old.php';
session_start();
$appuser = $_SESSION['LOGIN_user'];
$username = $_SESSION['LOGIN_STATUS'] ;
$imei = $_REQUEST["imei"];
echo $imei;
$conn->close();
?>
I am not able to get return values(imei) in response . How can i get / test values ?

Related

AJAX not working, success function is working?

the success function is working but the data is not going in the database
$(document).ready(function() {
$("#ChatText").keyup(function(e){
if(e.keyCode == 13) {
var ChatText = $("#ChatText").val();
$.ajax({
type:'POST',
url:'InsertMessage.php',
data:{ChatText:ChatText},
success:function(){
$("#ChatText").val("");
}
});
}
});
setInterval(function(){
$("#ChatMessages").load("DisplayMessages.php");
},15000000);
$("#ChatMessages").load("DisplayMessages.php");
});
PHP
<?php
session_start();
include "connectToDB.php";
if(isset($_POST['ChatText'])){
$uid = $_SESSION['userid'];
$gid = $_SESSION['GameId'];
$ct = $_POST['ChatText'];
$sql = "INSERT INTO `chats`( `ChatUserId`, `chatGameId`, `ChatText`) VALUES ('$uid','$gid',$ct);";
$result = mysqli_query($_db , $sql);
}
?>
one thing u could do to debug is that echo your sql query and see if you get the correct query that works. You can event try out that query in phpMyAdmin and see whats going on. Hard to tell anything without debug.

How to use a div id as a php var

I have the following function that pass to the div with id = "window", the value dbdata from the database.
$(document).ready(function() {
setInterval(function () {
$('#window').load('crt.php');
)};
)};
How can I get the id value and use it in the php code below?
without refresh the page?
if ($id = $row["dbdata"]) { do something }
I added the rest of the code:
index.html
`<div id = "window"></div>`
`if ($id = $row["dbdata"]) {do something}`
`<script type="text/javascript">
$(document).ready(function() {
setInterval(function () {
$('#window').load('crt.php')
}, 3000);
});
</script>`
crt.php
`<?php
$conn = new mysqli('localhost', 'root', '', 'user');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$result = $conn->query("SELECT id FROM data");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row['dbdata'] . '<br>';
}
}
?>`
Thank you in advance.
You can pass values in URL parameters.:
$('#window').load('crt.php?id=window')
Then in your php code you can retrieve the parameter using:
$id = $_GET["id"];
a simple way could be assign the GET param in your jquery load url and use it in your php code
$(document).ready(function() {
setInterval(function () {
$('#window').load('crt.php?id=window')
.
if ($_GET['id'] = $row["dbdata"]) { do something }
or you instead of load could use ajax get eg :
$.get( "crt.php?id=window", function( data ) {
alert( "Data Loaded: " + data );
});
in your crt.php
if ($_GET['id'] = $row["dbdata"]) {
return "hello!";
}

Ajax Form Validate Php Insert Query

i'm trying to add data to database with jquery. My code is here, but its not working.
Using bootstrap form, with that form i'm trying to send input datas to specific page (in this situation, adduser.php) in that page, i'm trying to check this values to database for be sure there is no same data (i'm checking email adresses)
Can you help me guys?
<script>
$(document).ready(function(){
$('#adduser').click(function(){
var add_name = $('#add_name').val();
var add_surname = $('#add_surname').val();
var add_email = $('#add_email').val();
var add_password = $('#add_password').val();
if(add_name == '' || add_surname == '' || add_email == '' || add_password == '' ){
$('#add_user_error').html("<strong class='text-danger'>*** Please enter all details</strong>");
}else{
$.ajax({
url: "adduser.php",
method: "post",
data:{add_name:add_name,add_surname:add_surname,add_email:add_email,add_password:add_password},
success: function(data){
if (data == 1) {
$('#add_user_error').html("<strong class='text-danger'>This email have in database</strong>");
}else{
$('#add_user_error').html("<strong class='text-success'>Success</strong>");
}
}
}); return false;
}
});
});
</script>
<?php
include ('setup.php'); //Database connection dbc
if(isset($_POST['adduser'])){
$add_name = $_POST['add_name'];
$add_surname = $_POST['add_surname'];
$add_email = $_POST['add_email'];
$add_password = $_POST['password'];
$q = "SELECT * FROM users WHERE email = '$add_email'";
$r = mysqli_query($dbc, $q);
$adduser = mysqli_fetch_assoc($r);
if($adduser['email'] !== $add_email){
$q = "INSERT INTO users (name,surname,email,password) VALUES ('$add_name','$add_surname','$add_email','$add_password')";
$r = mysqli_query($dbc, $q);
}
}
?>
There could be any other mistake too but I think you need to send adduser from ajax. Then only $_POST['adduser'] will be true (it is false now, as it isn't set)
Replace your data line with line below and try
data:{add_name:add_name,add_surname:add_surname,add_email:add_email,add_password:add_password,adduser:1}
In your ajax code pass your form data using serialize method
$.ajax({
url: "adduser.php",
method: "post",
data:$('#yourformid').serialize(),
/* your remaining code as it is */

php array data not working in JQuery

The problem is in jQuery, jsondata.uname is not displaying any result. If I echo $myarray in PHP and use data in jQuery it will display the result in array form.
$(document).ready(function() {
$('button#myloginbtn').on('click', function(e) {
e.preventDefault();
var username = $('input#myusername').val();
var password = $('input#mypassword').val();
$.post('home.php', {
domylogin: 'domylogin',
username: username,
password: password
}, function(data) {
jsondata = JSON.parse(data);
$('p#auser').text(jsondata.uname); // This is not working
});
});
});
home.php:
<?php
if (isset($_POST['domylogin'])){
$myuname = $_POST['username'];
$mypword = $_POST['password'];
// temporary testing, later i will return some data from database related to these values
$myarray = array();
$myarray['uname'] = $myuname;
$myarray['pword'] = $mypword;
echo json_encode($myarray);
}
?>
Thanks everyone for support, I had got the problem. I had some html code in my php file, on removing html code in 'home.php' everything works as supposed!
In your PHP file:-
$myarray = array();
if (isset($_POST['domylogin'])){
$myuname = $_POST['username'];
$mypword = $_POST['password'];
$myarray['uname'] = $myuname;
$myarray['pword'] = $mypword;
}
echo json_encode($myarray);
Write your AJAX call as below:-
$.post('home.php', {
domylogin: 'domylogin',
username: username,
password: password
})
.done(function( data ) { // Use done function
jsondata = JSON.parse(data);
if(jQuery.isEmptyObject(jsondata) == false) // check jsondata is empty or not
console.log(data);
$('p#auser').text(jsondata.uname);
});
Hope it will help you:-

jquery ajax request fails

I'm new to web programming and I'm trying to select value for a selector box based on value selected in another selector box. Below is the jquery code snippet and php script for your reference. thanks for the help in advance.
jquery-ajax code
$(function(){
$("#tier").change(function(){
var formdata = {'tierval':document.getElementById("tier").value};
alert("Selected Tier : "+document.getElementById("tier").value);
$.ajax({
type: "POST",
url: "/includes/getplayers.php",
data: formdata,
success: function(data)
{
alert("called getplayers.php");
$("#opp").empty();
$("#opp").html(data);
},
error:function()
{
alert('ajax failed');
}
});
});
});
php script:
<?php
if (isset($_POST['tierval']))
{
$tier = $_POST['tierval'];
}
$query = "select PlayerID,First_Name from GOFTEL.PLAYER where TierID = $tier";
$result = mysqli_query($pconn, $query);
$row = mysqli_fetch_assoc($result);
while($row)
{
echo '<option value=\"$row[PlayerID]\" > $row[First_Name] </option>';
}
/* free result set */
$result->free();
/* close connection */
$pconn->close();
?>
Since you are already using jQuery consider the below:
var formdata = {'tierval':$("#tier").val};
alert("Selected Tier : "+$("#tier").val);
// or
console.log(formdata); // using the browser console
// or
alert( this.value );
// or
console.log($(this).val())
then you need to output something in the php script, so that #opp actually gets populated with the result, so
$query="select PlayerID,First_Name from GOFTEL.PLAYER where TierID = $tier";
$result=mysqli_query($pconn, $query);
$row=mysqli_fetch_assoc($result);
print $row['First_Name'];
Then evaluate what you get in the console or alerts and go from there.
You need to use below code in php script
while($row=mysqli_fetch_assoc($result)){
echo "<option value=" . $row[PlayerID]. ">" . $row[First_Name] . "</option>";
}

Categories