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...