pass info after ajax submission
12-28-2011, 08:00 PM
|
pass info after ajax submission
|
Posts: 96
Name: Joan
|
Hello, i think this a question with ajax javascript and php..
i'm going crazy trying to figure this out if anyone can help me i would really appreciate it...
here is the scenario i have confirm.php with the form. on submit button it calls a reserve.php page via ajax.. on confirm page after the submission of the form and after the reserve page is called i have this code
setTimeout("window.location.href='logistica_list.p hp'",10000);
when the user submits the form it redirects to a new page after 10 second of form submitted, this works fine. the problem is that sometimes after submitting the form the user may have error in that case i need the page not to do the redirection. so i tried this on reserve.php
if(!$errors){
echo "<b>RESERVATION HAS BEEN ADDED TO THE CALENDAR</b>";
echo '<script> var submitted = "OK"; </script>';
}
and then on the confirm.php i tried to get the variable submitted
if(submitted == "OK"){
setTimeout("window.location.href='logistica_list.p hp'",10000);
}
but it doesnt work i dont really know ajax and just a little of javascript, so i dont know if this is even possible.
thanks inadvance and any help will be greatly appreciate it
|
|
|
|
12-28-2011, 08:34 PM
|
Re: pass info after ajax submission
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Make your reserve.php file return a result as a JSON object instead. JSON objects can be directly parsed in javascript, which means you can i.e. receive variable data from your ajax calls.
You can easily (and should) find this through google, but a JSON object is basically a string with a special syntax. PHP also has a function (I think it's included in the standard php installation) json_encode() which will build a JSON string from pretty much any type of data (variables, arrays, objects). Evan if you don't have access to json_encode(), you can just build the string yourself. Plenty of examples to find.
Then you echo only that JSON string to the page, instead of your "RESERVATION HAS..." etc., so that your javascript receives it. You can then parse it into a JSON object and use it in your javascript, like you were trying to do above.
To parse it, you can use eval(). However, from my knowledge one should never use eval(), since poeple can then make your script run code that it isn't supposed to run. Instead, you should use some library (I know i.e. that jQuery has a JSON parser, all libraries probably does).
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
12-29-2011, 08:22 AM
|
Re: pass info after ajax submission
|
Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
|
javascript variables only exist on the page where they were created.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
12-29-2011, 02:24 PM
|
Re: pass info after ajax submission
|
Posts: 96
Name: Joan
|
thanks for your help..
ok on reserve.php page i did this
$passed["submitted"] = "OK";
$fver = json_encode($passed);
but now how can i get that value in confirm.php ??
Last edited by stivens; 12-29-2011 at 03:01 PM..
|
|
|
|
12-29-2011, 04:21 PM
|
Re: pass info after ajax submission
|
Posts: 185
Location: print_r($serbia);
|
As lizciz said above, you need to echo back the json encoded array:
PHP Code:
$passed["submitted"] = "OK";
$fver = json_encode($passed);
echo $fver;
After the page is loaded and you send an ajax request, you are done with php. Everything that is returned can be only used with javascript, because php is not event oriented.
So what ever you are planing to do on your confirm.php page, you need to do it with javascript.
|
|
|
|
12-29-2011, 04:38 PM
|
Re: pass info after ajax submission
|
Posts: 96
Name: Joan
|
Quote:
Originally Posted by miki86
As lizciz said above, you need to echo back the json encoded array:
PHP Code:
$passed["submitted"] = "OK";
$fver = json_encode($passed);
echo $fver;
After the page is loaded and you send an ajax request, you are done with php. Everything that is returned can be only used with javascript, because php is not event oriented.
So what ever you are planing to do on your confirm.php page, you need to do it with javascript.
|
thanks...
yes my questions is how can i get this $fver in confirm.php with javascript?
|
|
|
|
12-29-2011, 08:06 PM
|
Re: pass info after ajax submission
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
This is all very easy to find on the internet through google. For example, look at json.org, or more specifically http://www.json.org/js.html
The result you get from the ajax call is still just a text string, which should look like this
{ "submitted" : "OK" }
You just need to parse it, to make it an actual object. Again, you could use eval(), but... (read on the link above). I your case I think it's safe to use eval(). The following is copied from json.org
Code:
var myObject = eval('(' + myJSONtext + ')');
where myJSONtext is the result you receive through your ajax call. After that, you can use myObject as any other object, i.e.
Code:
if (myObject.submitted == "OK") {
// ...
}
EDIT: By the way, I do believe that "submited" is spelled with only one "t" 
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
Last edited by lizciz; 12-29-2011 at 08:08 PM..
|
|
|
|
12-30-2011, 01:46 PM
|
Re: pass info after ajax submission
|
Posts: 96
Name: Joan
|
Thanks so much lizciz
i understand what you said, there is something that i dont get and it is how the whole passing data back and forth works.
i will try to explain on confirm.php page there is the function that is going to send the data to reserve.php here is the function
PHP Code:
function AjaxCallForm(){
doAjax('reserve_removal.php','Reserve_DateMM='+ document.getElementById('out_Lamp_Chapmonth').value
+ '&Reserve_DateDD=' + document.getElementById('out_Lamp_ChapDay').value
+ '&Reserve_DateYY=' + document.getElementById('out_Lamp_ChapYear').value
+ '&Case=' + document.getElementById('case_n').value
+ '&Deceased=' + document.getElementById('deceased_name').value
+ '&Start_Time=' + document.getElementById('stTime').value
+ '&End_Time=' + document.getElementById('endTime').value
+ '&Driver_Id=' + document.getElementById('radioGroupTwo').value
+ '&Type=' + document.getElementById('type').value
+ '&Consulate=' + document.getElementById('consulate').value
,'displayMYmessage','get',0,'progress');
var myJSONtext = ?????;
var myObject = eval('(' + myJSONtext + ')');
if (myObject.submitted == "OK") {
setTimeout("window.location.href='mortuary/logistica_list.php'",10000);
}
}
//then the button that calles the function the submit button
<input type="button" name="BtnRsvp" id="BtnRsvp" onClick="AjaxCallForm();return disablesubmit();" value="Make Reservation" class="button" />
ok that is the function.. then on reserve.php page i process the information save the data in the DB and then at the end i send back to confirm if the submission went through ok.
PHP Code:
if(everything went ok blah blah){
echo '</br><font size=2 color=#66CC66><div align=center>New Reservation Details Suceessfully Saved ..!<a href="http://latino-americana.com/calendar">CLICK TO VIEW</a></div></font>';
echo "<b>RESERVATION HAS BEEN ADDED TO THE CALENDAR</b>";
$passed["submitted"] = "OK";
$fver = json_encode($passed);// if i echo this it just shows on the page { "submitted" : "OK" }
}
so on confirm page where do i get $fver or how do i get it?
i tried adding it to the function that calls the form but the thing is that i dont really know how passing the information works..
im sorry i'm not getting it. but thanks so much for your help
|
|
|
|
12-30-2011, 09:52 PM
|
Re: pass info after ajax submission
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
An AJAX call will work like this:
* The user's browser loads the confirm.php page.
* From there, you make the AJAX call, which will send a normal HTTP request to the server "behind the scenes", without reloading the page.
* That call requests the reserve.php page, which will process the data, store in DB and so on.
* All the work you've now done on reserve.php lies on the server entirely. The user's browser know nothing of what have happened, cannot in any way see the php variables, and is just waiting for a response.
* reserve.php now returns it's result by just printing out (echo) a JSON string, nothing else.
* That response is received by the browser, and can be processed by javascript, NOT php!
* So, in your javascript, you check the JSON-object to see if the parameter "submited" has the value "OK". If it does, you can print your message (<b>RESERVATION HAS...), with javascript, not php.
What you're probably getting stuck at is that request.php is supposed to just print the JSON string:
{ "submitted" : "OK" }
Nothing else. Then it's up to the javascript the receive it and to do something with it.
EDIT:
Of course, you could do this without using JSON, by just printing any string and have your javascript code work with that instead (i.e. printing "OK", and let your javascript check if the received text equals "OK"). The nice thing about JSON is that it's a standardized and easy format, that can handle different data types and is supported by both php and javascript. In your case, when sending just "OK", it doesn't matter much. But in more general cases, where you send more data, JSON has the upper hand.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
Last edited by lizciz; 12-30-2011 at 10:02 PM..
|
|
|
|
12-31-2011, 04:30 AM
|
Re: pass info after ajax submission
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Quote:
Originally Posted by DevantaE2
i dont know how to do it please teach me how
|
Google it...
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
12-31-2011, 05:51 PM
|
Re: pass info after ajax submission
|
Posts: 96
Name: Joan
|
I have google it a lot and still can't get it to work.. :/ im stuck in how to receive it in javascript. i print {"submited":"OK"} but then how do i get it in confirm.php if the page has already loaded? do i need to make a function? do i have to make another ajax call from receive.php to confirm.php? i just dont get it..
PHP Code:
//THIS IS ON RESERVE.PHP
$passed["submitted"] = "OK";
$fver = json_encode($passed);
echo '<script type="text/javascript"> var myJSONtext = '.$fver.';</script>';
//THIS IS ON CONFIRM.PHP
var myObject = eval('(' + myJSONtext + ')');
if (myObject.submitted == "OK") {
setTimeout("window.location.href='mortuary/logistica_list.php'",10000);
}
it shows an Error: myJSONtext is not defined.. what do i do?
|
|
|
|
01-01-2012, 07:55 AM
|
Re: pass info after ajax submission
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Oh dear, you have it all backwards
On reserve.php you should echo the json string, nothing else.
PHP Code:
$passed["submitted"] = "OK"; $fver = json_encode($passed); echo $fver;
On confirm.php, you need to work with your javascript, not the php code. Since you have already managed to do the ajax call, I presume you have some function for that. It would probably help if you post your javascript code here. You'll want to use the eval() stuff I copied from json.org (again, in your javascript, not php) after the ajax call has been performed and returns the string.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
01-01-2012, 09:28 AM
|
Re: pass info after ajax submission
|
Posts: 185
Location: print_r($serbia);
|
You need to catch the response with javascript.
I suggest you look for jQuery library, or my personal favorite mootools, especially if you are javascript beginer.
Documentation is great for both of the above.
Im using mootools so i'll give you an example of how ajax request looks:
PHP Code:
var AjaxReq = new Request({
url: 'ajax/process_request.php',
data: regForm.toQueryString(),
method: 'post',
onRequest: function(){
// Do something
},
onComplete: function(responseText) {
var data = JSON.decode(responseText); // This is the part where you catch the php response
switch(data.regCheck) {
case 'ok':
// Do something
break;
case 'bad':
// Do something
break;
}
},
onFailure: function(){
// Do something
}
});
AjaxReq.send();
There is also a HTML request, where you build your html from the requested php page and then just adopt the response with javascript.
Of course you can do this with pure javascript but it's gonna take more lines of code than this.
|
|
|
|
|
« Reply to pass info after ajax submission
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|