I have two files, one for HTML and jQuery and other is for PHP.
I am getting response from PHP page, but I am not able to display in a HTML text box.
Here is my code:
<script type="text/javascript">
function fill_amt()
{
var sname=$("#sname").val();
$.post("try_insert.php",{sname:sname,cmd2:"fill_amt"},function(data){
$("#tot_amt").html(data);
});
}
</script>
Here is my HTML code:
<input type="text" id="tot_amt" name="tot_amt">
Here is my PHP code
$sql = mysql_query("select total_fee from admission where first_name='".$sname."'")or die(mysql_query());
$val=mysql_fetch_row($sql);
$tot=$val;
echo json_encode($tot);
Use:
$("#tot_amt").val(data);
instead of
$("#tot_amt").html(data);
Also format your json data correctly.
Your JS code should be
<script type="text/javascript">
function fill_amt()
{
var sname=$("#sname").val();
$.post("try_insert.php",{sname:sname,cmd2:"fill_amt"},function(data){
var json=JSON.parse(data); // parsing data form JSON string and convert it into object
$("#tot_amt").val(json.total);
});
}
</script>
PHP code should be
$sql = mysql_query("select total_fee from admission where first_name='".$sname."'")or die(mysql_query());
$val=mysql_fetch_assoc($sql);
$tot=$val['total'];
echo json_encode(array("total"=>$tot)); // give response as json string
Related
im try to store random number after click on button and store in database table field name 'f_id'. when i click on button, the new random number is generated but its not store in database table field name 'f_id'.. my code is..
my jquery code ////
<script language="javascript" type="text/javascript">
jQuery(document).ready(function($){
$(".random").click(function(){
var number =Math.floor(Math.random() * 100000);
console.log(number);
});
});
</script>
and my html code ////
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<a class="random action btn btn-success btn-sm" href="#" >random number</a>
</body>
</html>
plzz tell me i have created database already but not store this value number on database table..
php code...
<?php
function mytable($number){
$result = mysqli_query( $this->conn,"INSERT INTO school(f_id) VALUES ('$number')");
return $result;
}
?>
mytable($number);
how to connect jquery var number in php code mytable under variable number... plzz solved
You need to call your PHP function using ajax (https://api.jquery.com/jquery.post/) and send your random number.
You can then access it via $_POST['number'] on the server side.
$.post( "mytable.php", function({number: number}) {
});
Php:
function mytable($number)
$result = mysqli_query( $this->conn,"INSERT INTO school(f_id) VALUES ('$number')");
return $result;
}
mytable($_POST['number']);
You need to call your php backend script. This could be done like this:
<script language="javascript" type="text/javascript">
jQuery(document).ready(function($){
$(".random").click(function(){
var number =Math.floor(Math.random() * 100000);
console.log(number);
window.location.href='http://path.to/your_script.php?rnd=' + number
});
});
</script>
And in your_script.php:
<?php
function mytable($number){
$number=intval($number);
$result = mysqli_query( $this->conn,"INSERT INTO school(f_id) VALUES ('$number')");
return $result;
}
if(mytable($_GET['rnd'])) {
echo "It works";
}
else echo "Something went wrong";
I but putting a random number as id is "probably not" recommended...
I'm new to Json, Ajax and Jquery, so this is turning out very frustrating.
I'm doing a code to autocomplete on a form using Jquery. My form consists in 4 elements:
1) Client
2) Requestor
3) Address
4) Email
So far, I can autocomplete the Client with this code:
<script type="text/javascript">
$(document).ready(function(){
$("#Client").autocomplete({
source:'getClient.php',
minLength:1
});
});
</script>
However, I need to get the next data (Requestors, Address...) using the Client's values instead of showing all the requestors and all the addresses from the database.
I've been trying for 3 hours and I just don't seem to get any data.
As an example, I've just set my Requestor input with an onclick.
<input type="text" name="Req" size="60" id="Reqt" onclick="getreq()"><br>
This was the last code attempt I've done so far:
<script type="text/javascript">
$(document).ready(function()
{
function getreq(){
//get the username
var Client = $('#Client').val();
//use ajax to run the check
$.ajax({
data: Client,
url: 'getReq.php',
type: 'post',
success: function (response) {
$("#Reqt").autocomplete({
source:'getReq.php',
minLength:1
});
}
});
}
});
</script>
And my PHP looks like this
<?php
mysql_connect("localhost","user","pass");
mysql_select_db("test");
$client = mysql_real_escape_string($_POST['Client']);
$query=mysql_query('SELECT * FROM client WHERE Clients ="'.$client.'"');
$json=array();
while($Reqt=mysql_fetch_array($query)){
$json[]=array(
'value'=> $Reqt["Requestor"],
'label'=>$Reqt["Requestor"]
);
}
echo json_encode($json);
?>
Like I said, I've been trying this for hours and I'm pretty new to Json, Ajax and Jquery, but I need to use those technologies, I would really appreciate any and all insight. Thanks in advance!
When my page opens I call a PHP file and output a form in the form of an echo.
A simplified example below:
echo "<table id='results'>";
echo "<form method = 'post'><input id='".$row['batchname']."'><button class='btn1'>Query this record</button></form>";
</table>
There will be many versions of the above form as table rows are pulled from the database.
I am usin AJAX to handle the output:
$(document).ready(function(){
$(".btn1").click(function(e){
e.preventDefault();
var bname = ("#<?php echo $row['batchname'];?>").val();
$post(
"somephp.php",
{name : bname},
function(response, status){
$("#results").replaceWith(response);
}
);
});
});
When I input an non PHP ID into the jQuery the AJAX work but I always post the first returned row for every form produced as the ids output in the PHP are the same. Can I echo PHP variable into jQuery like this? Is there a better way of getting dynamic ID's into jQuery.
Rather than hacking about like this, do it properly.
$(".btn1").click(function(e) {
e.preventDefault();
var form = $(this).parents("form");
var value = form.find("input")[0].value; // or something else here
});
I've got a JSON value that has been converted from a JavaScript object using JSON.stringify. I'm trying to parse the contents of the JSON using PHP, but I haven't had any luck. I'm sure I'm doing something really basic wrong.
In file1.php, I've got something like:
<html>
<head>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script src='/json2.js'></script>
<script>
var irxmlnewsreleases = new Array();
irxmlnewsreleases[0]={
"attachmentfileid":12039
};
var news_release = JSON.stringify(irxmlnewsreleases);
$(document).ready(function () {
$("#response").text(news_release);
});
</script>
</head>
<body>
<div id="response"></div>
</body>
</html>
I'm then trying to read this data from file1.php using json_decode in file2.php.
I tried first (wrongly) using file_get_contents and have been bashing at this for a while without success. I guess the issue is obviously that the JSON value doesn't exist until the JavaScript is run, so PHP is of course never able to read the value of the jQuery-generated div content. What I don't know is how to get that value.
The JSON is being generated successfully in file1.php and is valid JSON (I've run it through jsonlint).
What's a better way of getting the value of that dynamically-generated JSON into PHP?
$(document).ready(function () {
$("#response").text(news_release);
$.post('file2.php', { php_post_var1: news_release }, function (data) {
//do something with the PHP script output here if you want
});
});
Then in your PHP script file2.php do something like
<?php
$news_release = $_POST['php_post_var1'];
echo 'PHP received ' . $news_release;
?>
I want to display an image as a response to an ajax call using php.. Does anybody have the complete code for this?? Please help me out..
Thanks in advance..
Well I don't have the complete code to this, and if I did, I wouldn't give it to you.
You need to start off by creating an image using PHP's various image generation functions. To make it dynamic you could send it various parameters which are encoded in the url and can be fetched using the $_GET superglobal in php.
You would then set the src of an existing image placeholder element to the location of your dynamic image, then voila! Instant dynamic image.
You should use jQuery + JSON combination. You can convert php array into JSON format using json_encode.
index.php:
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<a href='car.php' class='ajax'>Car Image</a>
<a href='bike.php' class='ajax'>Bike Image</a>
<div id="title">Title comes here</div>
<div id="image">Image comes here</div>
car.php:
<?php
$jsonArray['title'] = "Car";
$jsonArray['image'] = "<img src='images/car.jpeg'>";
echo json_encode($jsonArray);
?>
bike.php:
<?php
$jsonArray['title'] = "Bike";
$jsonArray['image'] = "<img src='images/bike.jpeg'>";
echo json_encode($jsonArray);
?>
ajax.js:
jQuery(document).ready(function(){
jQuery('.ajax').click(function(event) {
event.preventDefault();
jQuery.getJSON(this.href, function(snippets) {
for(var id in snippets) {
jQuery('#' + id).html(snippets[id]);
}
});
});
});