solon Posted November 1, 2008 Share Posted November 1, 2008 Hey guys i was wondering if its possible to send two different values from a php file through AJAX to another php file! If it is possible how do i send it and how to i get it in the second php file! Thanks in anvance! Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/ Share on other sites More sharing options...
RichardRotterdam Posted November 1, 2008 Share Posted November 1, 2008 Yes you can. However I am guessing you're not understanding the basics of ajax. You use javascript to make a call to an external file not php. for ajax you usually use 2 pages 1. the main page.(this can be a simple html file) 2. the ajax page/script.(usually a serverside script but can be a simple text file as well ) the main page makes a call to another page/script using javascript this main page can send values using the Get method or Post method for example var url='ajax_page.php?val1=first&val2=second'; this will send the values first and second. the ajax_page.php can return either plain text/html or json(which are javascript object) it can return xml too but hardly anyone does that these days. now for your solution you could use json to return 2 values which you then use to on your main page. as for the tiny bit that has to do with php. if youre using php 5.2 or above there is this handy function to transfrom a php array to json. this function is named json_encode good luck Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679944 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 thanks for your response but i think that this will not help me! Ok so i have a .php script which includes the ajax-javascript script and through that file i want to send to variables to the other php script so i can proccess the and return the result depending on the clients input in a textbox! If you need any further details just ask! Thanks again Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679960 Share on other sites More sharing options...
marcus Posted November 1, 2008 Share Posted November 1, 2008 @Dj Kat You're better off sending the URL and the params separately. var url = 'page.php'; var params = 'val1=something&val2=somethingelse'; Then you would open the URL with post or get, and lastly send the params. var http; // then make you necessary xml request type var url = 'page.php'; var params = 'val1=something&val2=somethingelse'; http.open("POST",url,true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = somefunction; http.send(params); Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679961 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 ok guys because i am really noobie on ajax i will give you a specific example: lets say i have this: file1.php(which includes the ajax script) <form method='post' action='some.php'> <input type='text' name='stake' size='10' onkeyup='get_total(this.value)' /> <input type='text' name='poss_win' id='poss_win' value='' size='10' /> </form> and this: file2.php(which needs the value of both textboxes above to process) <?php //some php script ?> my question is how to pass both those values from file1.php to file2.php so the can be processed in file2.php? Thanks again Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679965 Share on other sites More sharing options...
RichardRotterdam Posted November 1, 2008 Share Posted November 1, 2008 I still think this should be moved to Ajax help. @Dj Kat You're better off sending the URL and the params separately. var url = 'page.php'; var params = 'val1=something&val2=somethingelse'; Then you would open the URL with post or get, and lastly send the params. I know it was only to clarify you can use the get method and not to give a working example. @solon Maybe the info was a bit too much to start with I suggest you start some ajax tutorials or you could simply use a javascript framework to make things a lot easier. The code you posted is only html the interesting stuff happens in your get_total() function can you post that? Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679970 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 this is all the script! I found it on the internet in some tutorials and i changed a few things! Maybe there are some errors in it! <script> var xmlHttp function get_total(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var session = document.getElementById('session').value; var stake = document.getElementById('stake').value; var total = document.getElementById('total').value; var queryString = "?session=" + session + "&stake=" + stake + "&total=" + total; xmlHttp.open("GET","amount_check.php" + queryString, true); xmlHttp.send(null); } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.amount.poss_win.value=xmlHttp.responseText; } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }</script> Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679979 Share on other sites More sharing options...
RichardRotterdam Posted November 1, 2008 Share Posted November 1, 2008 I think mgallforever's code was a lot tidier to start with. but this will get you on the right track. first change your form like so(only added the id='stake)' <form method='post' action='some.php'> <input type='text' name='stake' id='stake' size='10' onkeyup='get_total()' /> <input type='text' name='poss_win' id='poss_win' value='' size='10' /> </form> then change your javascript to function get_total(){ /** *check browser for the correct xmlHttp request */ var http; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari http = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } /** *the url is the ajax page to be called in this case page.php *params are the values that are going to be send */ var url='page.php'; //get the value of the stake input element var stake=document.getElementById('stake').value; //get the value of the poss_win input element var poss_win=document.getElementById('poss_win').value; var params = 'stake='+stake+'&poss_win='+poss_win; http.open("POST",url, true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = function(){ if(http.readyState == 4){ updateContent(http.responseText); } } http.send(params); } /** *this is the function that handles what is going to be updated on your main page */ function updateContent(text){ console.log(text);//this will output the data to firebug } read the comments i made in this script that will make things more clear on how it works Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679988 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 Using the following code i got the results correctly into firebug but it does not update the value of the textbox in the code below: <script> var xmlHttp function get_total(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var session = document.getElementById('session').value; var stake = document.getElementById('stake').value; var total = document.getElementById('total').value; var queryString = "?session=" + session + "&stake=" + stake + "&total=" + total; xmlHttp.open("GET","amount_check.php" + queryString, true); xmlHttp.send(null); } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.amount.poss_win.value=xmlHttp.responseText; } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }</script> this is the part of the code that was supposed to update the textbox right? function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.amount.poss_win.value=xmlHttp.responseText; } } At this point thats my only problem! If you can find the solution i would be very thankful Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679990 Share on other sites More sharing options...
RichardRotterdam Posted November 1, 2008 Share Posted November 1, 2008 if that's the only thing then try function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById('poss_win').value=xmlHttp.responseText; } } Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679992 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 nop it still does not work! Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679995 Share on other sites More sharing options...
RichardRotterdam Posted November 1, 2008 Share Posted November 1, 2008 sorry man the getElementById was incorrect now it is correct document.getElementById('poss_win').value=xmlHttp.responseText; Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-679996 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 still nothing! Should i mentioned that all the text boxes including the poss_win are 'echod' in php? i dont think that would make a difference but you never know! Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-680000 Share on other sites More sharing options...
RichardRotterdam Posted November 1, 2008 Share Posted November 1, 2008 you said firebug gives the right output correct? what does the html form look like? does your form have this in it? <input type='text' name='poss_win' id='poss_win' value='' size='10' /> and what error do you get if you get one. another thing you never called the function stateChanged() try putting an alert inside it you will notice it doesnt do anything because it is never being called Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-680002 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 Yes that is in my form! Something i forgot to mention before is that firebug gives me this in the response tab: Firebug needs to POST to the server to get this information for url: ../amount_check.php?session=5c5af9950df85f176216eaa228fd62f3&stake=44&total=9.72 This second POST can interfere with some sites. If you want to send the POST again, open a new tab in Firefox, use URL 'about:config', set boolean value 'extensions.firebug.allowDoublePost' to true This value is reset every time you restart Firefox This problem will disappear when https://bugzilla.mozilla.org/show_bug.cgi?id=430155 is shipped. Load Response (as a button) and after i click it it gives me the response correctly Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-680005 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 Where should i call the stateChanged() function though? Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-680007 Share on other sites More sharing options...
GingerRobot Posted November 1, 2008 Share Posted November 1, 2008 You don't call it explicitly; rather you specify the function to be called when the stage is changed. Assuming you are still using the last code you posted, you need to add this line: xmlHttp.onreadystatechange=stateChanged; After this line: xmlHttp.open("GET","amount_check.php" + queryString, true); Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-680076 Share on other sites More sharing options...
solon Posted November 1, 2008 Author Share Posted November 1, 2008 Thanks guys! I atually had a small error in my php syntax and that was the reason i didnt get any result! Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/130978-solved-send-two-different-values-from-php-through-ajax-to-php/#findComment-680079 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.