I am trying to load the values from mysql using php and js. Following is function which called when a button is clicked.
<script type="text/javascript">
function ab(){
var MyJSNumVar = "<?php
$orderid = $_GET['orderid'];
$result="";
$db = mysqli_connect("localhost", "root", "");
mysqli_select_db($db, "mydba");
$sql = "SELECT amount FROM mydba.fl_placed_order where orderid='".$orderid."'";
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
while ($info = mysqli_fetch_array($result)) {
$result = stripslashes($info['orderid']);
}
echo $result;
?>";
}
</script>
Body code is
<button onclick="ab()">mittal</button>
In browser the page is empty with no error in console. Even i cannot see the button. What's wrong in this code...
Based on your expert advice i changed my code....
<script type="text/javascript">
function ab(){
$.ajax({
type: 'get',
url: "javascript.php/orderid=CF450AA4",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, status) {
alert("successful");
console.log(msg);
},
error: function (msg, status) {
console.log("failure");
console.log(msg);
alert("failure");
}
});
}
</script>
and my javascript.php file is
<?php
$orderid = $_GET['orderid'];
$result="";
$db = mysqli_connect("localhost", "root", "");
mysqli_select_db($db, "givem6la_shaleenmittal");
$sql = "SELECT amount FROM givem6la_shaleenmittal.fl_placed_order where orderid='".$orderid."'";
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
while ($info = mysqli_fetch_array($result)) {
$result = stripslashes($info['orderid']);
}
echo $result;
?>
Response is
Notice: Undefined index: orderid in C:\xampp\htdocs\javascript.php on line 2
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\javascript.php on line 11
I have no clue what is this...
For dataType: "json"
, the required result should be sent as json values from php script. You can use json_encode()
in php to convert your result to json
. In your script change it to
echo json_encode($result);
Also in your javascript
$.ajax({
type: 'get',
url: "javascript.php",
data: "orderid=CF450AA4", //add this line
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
error: function (response) {
console.log("failure");
console.log(response);
alert("failure");
}
});
Update: Also modify the line $info = mysqli_fetch_array($result)
to $info = mysqli_fetch_array($result,MYSQLI_ASSOC)
in your db script. And also
$sql = "SELECT amount,orderid FROM givem6la_shaleenmittal.fl_placed_order where orderid='$orderid'";
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$final_result = stripslashes($info['orderid']);
}
echo json_encode($final_result);
Try like this :- url: "javascript.php?orderid=CF450AA4"
<script type="text/javascript">
function ab(){
$.ajax({
type: 'get',
url: "javascript.php?orderid=CF450AA4",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, status) {
alert("successful");
console.log(msg);
},
error: function (msg, status) {
console.log("failure");
console.log(msg);
alert("failure");
}
});
}
</script>
EDIT :-
Your query is :-
$sql = "SELECT amount FROM givem6la_shaleenmittal.fl_placed_order where orderid='".$orderid."'";
you are selecting amount
field from your table and you are trying to do like this :-
while ($info = mysqli_fetch_array($result)) {
$result = stripslashes($info['orderid']);
}
you will not get value of $info['orderid']
since you are not retrieving value from your table. You have to select orderid
also from that table. May be this would work :-
$sql = "SELECT amount,orderid FROM givem6la_shaleenmittal.fl_placed_order where orderid='".$orderid."'";
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
while ($info = mysqli_fetch_array($result)) {
$result = stripslashes($info['orderid']);
}
echo $result;
I didn't test it, but I guess it will work properly.
In your client-side Javascript
and HTML
:
<button onclick="ab(<?php echo $_GET['orderid']; ?>)">mittal</button>
<script type="text/javascript">
MyJSNumVar = null;
function ab ( _orderid ) {
$.get("javascript.php", { orderid: _orderid }).done( function( data ) {
MyJSNumVar = data;
alert( MyJSNumVar );
});
}
// use MyJSNumVar anywhere else you wish ...
</script>
And in your server-side PHP
:
<?php
$orderid = $_GET['orderid'];
$db = mysqli_connect('localhost', 'root', '');
mysqli_select_db($db, 'mydba');
$sql = 'SELECT amount
FROM mydba.fl_placed_order
WHERE orderid = "' . $orderid . '"
LIMIT 1';
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
$info = mysqli_fetch_array($result, MYSQLI_NUM);
echo $info[0];
exit;