Sending data to php from jquery - php

have a question and I hope that you can help me with it. I have two file frame.js and up.php. frame.js take data from inputs (work correctly), and sent to up.php.
Frame.js
//some code
$.ajax({
type : "POST",
url: "up.php",
data: {
login: login;
pass: pass},
success : function() {
alert('success');
close_frame ();
}
});
up.php
<?php
echo('reg start'); //message to check that it work
echo ('<script>alert('reg start');</script>'); // another varient to check
if ((isset($_POST['login']))
//some code
?>
SO jquery sent information to up.php. But is up.php launch automatically or what I need to do to make the up.php to take the data from jquery file?

The up.php will run and all the data send via the ajax will be receive in the
$_POST
So basically you do need to do anything to make the data available in the php because it's already available
But notice that you are not doing anything with the response (the 2 echos) so you will have to open the firebug/console in order to see the prints

You can access any variables you send through $_POST in up.php.
You should know that jQuery has a built in function for POST ajax requests:
$.POST(url, data)
.done(function(response){
alert(response);
});

You should process response from up.php in success function. Something like $(body).html(arguments[0])

Related

PHP: Assigning an AJAX response value into PHP Variable

I've read all the articles but cant seem to get my ajax response into a PHP variable. Please can you advice. I want to assign rowid to a PHP variable.
$(document).on('click', '#updateid', function() {
var vallab = $('#idval').val();
var rowid;
$.ajax({
url:'a.php',
type: 'POST',
async: false,
data: {labid: vallab},
success: function(data){
// console.log(data);
rowid = data;
}
});
console.log(rowid);
return rowid;
});
my a.php code is below
<?php
# Fetch the variable if it's set.
$lab_id = (isset($_POST["labid"])) ? $_POST["labid"] : null;
echo $lab_id;
?>
I am getting the response back with the id, and want to use it on that page
I want to pass rowid into a PHP function so I need to get the value of rowid.
Please can you advice?
I cant seem to get my ajax response into a PHP variable
Well, the AJAX response came FROM a PHP file, right? So why don't you do whatever you need to do with the response right in that PHP file?
$.ajax({
url:'THIS IS YOUR PHP FILE',
type: 'POST',
data: {THIS IS THE DATA YOU SEND TO PHP},
success: function(data){
console.log(data); //THIS IS THE RESPONSE YOU GET BACK
}
});
You can't use it. Javascript is a scripting language which run in browser when the dom is loaded and elements are visible.
PHP is a serverside language and run on server before the page is loaded.
You need to understand the lifecycle of your application. Your php code executes once, it runs the full script from top to bottom when the page loads. At the point the script starts if can only access the post that came with the request (e.g if you clicked submit on a form then the 'action' of the form receives the post). Any number of things can happen in your script, but once it's finished the php is gone, and so is the post (in basic terms). So you no longer have any access to the php which created this page.
Ajax allows you to update a section of your page - it sends a request to your sever and runs some php code - you must understand that this is a new and separate request, so the new post submission only exists in the lifecycle of this new execution and is in now way linked to the page that has already finished loading. Now you could ask Ajax to call your original script, but that wouldn't affect your page at all because the page does not reload. What you would get is a strange looking response which you (probably) couldn't do anything useful with.
Ajax allows small specific changes to the page, so when you get your response (which I assume you get in a format you want since you don't ask about it and you have a console.log) you then need to do something with jQuery/javascript. Instead of returning rowid write a javascript function like :
function printRowId(rowid) {
$('#your html div id here').text('Row id is ' + rowid);
}
and then call it in your response:
$.ajax({
url:'a.php',
type: 'POST',
async: false,
data: {labid: vallab},
success: function(data){
// console.log(data);
rowid = data;
}
});
printRowId(rowid);
return rowid;
You can use Ajax to update your data, update your database and then reflect the changes on the current page, but you cannot use it to pass directly to the php that has already finished executing

AJAX returning data from a PHP page with code inside

I got the following ajax where I want to send data from a < form > to a PHP page, where the data will be stored in a DataBase and then I will return a password generated in the PHP to the AJAX, but seems that I can't succeed
$.ajax({
type:'POST',
url:'register.php',
data:$('#form_register').serialize(),
});
I'm new to AJAX and every answer I saw was just a PHP page with a few lines of code, it's impossible to use the JSON response in a PHP page with more code?
If you want to return some data from your register.php, you have to output it in this file:
<?php
// register.php
/* Do whatever has to be done here */
echo json_encode(<your data-array here>); // Encode the data you want to return here
In your script where the ajax request is made you have to handle the response from register.php
$.ajax({
type:'POST',
url:'register.php',
data:$('#form_register').serialize()
}).done(function(returnedData) {
// Process the returnedData
});

PHP: Multiple ajax request

I want a code for multiple ajax request. What happens actually is my first ajax request give me the response and in that responce function i m calling another function which having another website url. I want to send data to this new website using multiple ajax request.
Please help me out...
Thanks,
Prafulla
use global variables and manipulate your data in each request.
like:
var a;
$.ajax({
url:url_one;
success: function(data){ a =data; }
});
$.ajax({
url:url_two;
data: a //sends the data from the 1st request
success: function(data){
//do something with the data from the 2nd url
}
});
You can encapsulate your ajax call in function or event handlers as you need.
You can also manuipulate the data returned in variable a before sending it to the 2nd url.
Seems sloppy to me, but sould work.

How do i use JQuery Ajax to call a PHP file on the server side to return and execute javascripts?

The client website needs to do a cross-domain JQuery Ajax call to a php file on my server, the php file will query the database for a bunch of stored javascripts which then need to be sent back to the client and be executed on the client's website. This is what i have so far, haven't done the grabbing javascript from database yet and it works. Is this the best way to do this (assuming i can grab the javascripts directly from the database without adding the escape sequence when echo'ing back to the client)? Thanks.
This is what i have so far:
client side:
$.ajax({ url: "http://localhost:8888/test.php",
dataType: "script",
});
server side (test.php):
<?php
echo "alert(\"WORKS!\");";
?>
Review the ajax documentation and handle the success callback option on the ajax method:
$.ajax({
url: "http://localhost:8888/test.php",
dataType: "html",
success : function(data) { alert(data); }
});
As noted by Ricardo, your PHP script should echo HTML or some other content appropriate for your scenario.
See http://api.jquery.com/jQuery.get/
And http://api.jquery.com/jQuery.getJSON/

Getting a value from a variable , send to php to process and add that value to mysql database?

i am new to php and mysql.
How can i extract a VALUE from a JAVASCRIPT VARIABLE(i set) then send it to a PHP page that can read it and process it , the PHP will then insert the value into a table in MySQL database.
var A = "somevalue"
I have been researching but none of it give me a simple and direct answer . I saw some people uses JSON(which i am unfamiliar with) to do this.
Hopes someone can give me an example of the javascript/jquery , php code to this. Thanks!
You've asked for... a lot. But, this tutorial looks like it could help you.
(FYI -- I swapped out the original tutorial for one on ibm.com. It's better but far more wordy. The original tutorial can be found here)
I'm not pretty sure if it works but just try this. Your jQuery script shoul be like this:
$(function(){
var hello = "HELLO";
$.post(
"posthere.php",
{varhello: hello},
function(response){ alert(response); }
)
});
and "posthere.php" is like this:
$varhello = $_POST['varhello'];
echo $varhello . ' is posted!';
you should then get an alert box saying "HELLO is posted!"
What you need is Ajax. This is an example jQuery to use:
function sendData(data) {
$.ajax({
type: 'POST',
data: data,
url: "/some/url/which/gets/posts",
success: function(data) {
}
});
}
This will send post data to that url, in which you can use PHP to handle post data. Just like in forms.
If you have a form:
<form id="theformid">
<input type="text">
</form>
Then you can use jQuery to send the form submit data to that sendData function which then forwards it to the other page to handle. The return false stops the real form from submitting:
$("#theformid").submit(function(){
sendData($(this).serializeArray());
return false;
});
If you though want to send just a variable, you need to do it like this:
function sendData(data) {
$.ajax({
type: 'POST',
data: {somekey: data},
url: "/some/url/which/gets/posts",
success: function(data) {
}
});
}
Then when you are reading $_POST variable in PHP, you can read that data from $_POST['somekey'].
Inside the success callback function you can do something with the data that the page returns. The whole data that the page returns is in the data variable for you to use. You can use this for example to check whether the ajax call was valid or not or if you need to something specific with that return data then you can do that aswell.

Categories