biza Posted October 14, 2013 Share Posted October 14, 2013 hello, I am trying pass values from ajax to php without refresh the page, I have a drop down list, and when I choose an option,I want send a value to ajax . now to change values in the "form", I want, the value who I have send to the ajax transform in php variable. PHP: <? $result = mysql_query("SELECT id_subuniverso, subuniverso, ref FROM tbl_sub_universo ORDER BY subuniverso ASC"); echo'<select name="sub_uni" id="subuniverso" onchange="show();"> <option value="0" size="35">..........Seleccione o Sub-Universo..........</option>'; while( $row = mysql_fetch_array($result)){ if ($row[0]== $subcat_id)echo '<option selected="yes" value="'.$row[0].'">'.$row[2].' - '.$row[1].'</option>';elseecho'<option value="'.$row[0].'">'.$row[2].' - '.$row[1].'</option>'; } mysql_free_result( $result ); echo"</select>";?> AJAX <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script><script type="text/javascript">function show(){var option = document.getElementById('subuniverso').selectedIndex;var ref = document.getElementById('subuniverso').options[option].text.substr(0,3); alert( ref); // <- Retorna os dados em array dos option selecionados $.ajax({ url: 'inser_actividade.php', type: 'POST', data: {ref : ref}, cache: false, success: function(response) { alert('foi enviado com sucesso' + ref) }, } );};show();</script> Link to comment Share on other sites More sharing options...
Ingolme Posted October 14, 2013 Share Posted October 14, 2013 The data property contains the data that will be sent to PHP: data: {ref : ref}, This data is received in PHP as $_POST['ref'] Link to comment Share on other sites More sharing options...
biza Posted October 14, 2013 Author Share Posted October 14, 2013 Ingolme, i already put the following code: <?$ref = $_POST['ref'];echo $ref; ?> nothing Link to comment Share on other sites More sharing options...
Ingolme Posted October 14, 2013 Share Posted October 14, 2013 Try print_r($_POST) to see if anything got there at all. Link to comment Share on other sites More sharing options...
biza Posted October 14, 2013 Author Share Posted October 14, 2013 only array() Link to comment Share on other sites More sharing options...
justsomeguy Posted October 14, 2013 Share Posted October 14, 2013 When you use jQuery to send an ajax request, are you sure the method is called "type" instead of "method"? Link to comment Share on other sites More sharing options...
biza Posted October 14, 2013 Author Share Posted October 14, 2013 yes, the method is "type". Link to comment Share on other sites More sharing options...
justsomeguy Posted October 14, 2013 Share Posted October 14, 2013 I don't see an obvious problem. On the inser_actividade.php during the post request you're saying the $_POST array is empty? Are you using your browser's developer tools to verify that or have you added some code to the ajax response callback to print the response from PHP? Link to comment Share on other sites More sharing options...
biza Posted October 15, 2013 Author Share Posted October 15, 2013 justsomeguy , thank for your help, have been valuable I use firebug to verify , and on firebug i see my response, but I have too a print and a "echo" to print the PHP response , and nothing! Link to comment Share on other sites More sharing options...
justsomeguy Posted October 15, 2013 Share Posted October 15, 2013 Are you saying you see the correct response in Firebug, that it prints what it received from $_POST? Or the response in Firebug shows the empty array? You can also look at the request in Firebug to make sure it is sending the post data. Link to comment Share on other sites More sharing options...
biza Posted October 16, 2013 Author Share Posted October 16, 2013 justsomeguy, I see the correct response in firebug, but in the page i see empty array "array()", when I put print_r($_POST). Link to comment Share on other sites More sharing options...
justsomeguy Posted October 16, 2013 Share Posted October 16, 2013 I see the correct response in firebug, but in the page i see empty array "array()", when I put print_r($_POST). I'm confused, those should be the same thing. If you're trying to just open the page in a browser then you're not sending any post data to it, so that's why there's nothing in $_POST. The ajax request submits post data. If you want to test the PHP page in the browser without using ajax then you would need to build a small form that submits to the same page via post and fill out the form and hit submit to see the post data displayed. But if you're just opening the page in your browser to test it, there isn't going to be any post data. Link to comment Share on other sites More sharing options...
biza Posted October 17, 2013 Author Share Posted October 17, 2013 (edited) justsomeguy , I send you a picture with browser, is confused for me too.https://www.dropbox.com/sc/d6ek88viw680w5v/oBac1PmC1I Edited October 17, 2013 by biza Link to comment Share on other sites More sharing options...
justsomeguy Posted October 17, 2013 Share Posted October 17, 2013 It looks like you're on the console tab in Firebug. Click on the Net tab (or Rede) and look for the ajax request and response there. Link to comment Share on other sites More sharing options...
biza Posted October 17, 2013 Author Share Posted October 17, 2013 In net tab shows sending and response, and variable in ajax is sent but the php page does not return echo Link to comment Share on other sites More sharing options...
Ingolme Posted October 17, 2013 Share Posted October 17, 2013 So what does the response say? Link to comment Share on other sites More sharing options...
biza Posted October 17, 2013 Author Share Posted October 17, 2013 (edited) the response show the variable in debugger mode, in firebug console, but not in the web page. i put a "echo" to show me the "$ref" variable but, nothing! Edited October 17, 2013 by biza Link to comment Share on other sites More sharing options...
Ingolme Posted October 17, 2013 Share Posted October 17, 2013 If the response is showing the variable's value then the problem is how you're managing the response. In the success callback, see what's in response: I assume jQuery puts the text in the response variable. success: function(response) { console.log(response); } Link to comment Share on other sites More sharing options...
biza Posted October 18, 2013 Author Share Posted October 18, 2013 In success callback , a put this code: success: function(response) {alert(console.log(response)} And the alert show undefined Link to comment Share on other sites More sharing options...
Ingolme Posted October 18, 2013 Share Posted October 18, 2013 console.log doesn't return a value. Forget alert(), console.log will show the value in Firebug's console. Link to comment Share on other sites More sharing options...
biza Posted October 18, 2013 Author Share Posted October 18, 2013 Ingome , but I don't see nothing, I look up for the value in Firebug's console and nothing Link to comment Share on other sites More sharing options...
biza Posted October 23, 2013 Author Share Posted October 23, 2013 anyone know a way to pass values from ajax to php without refresh? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 23, 2013 Share Posted October 23, 2013 Ajax is the method to do that, ajax is the technique of using Javascript to send a request from the browser to the server in the background, just like a request that would happen when someone clicks on a link or submits a form (with a couple limitations). You can send a get or post request and the server will see it exactly like any other request the browser sends, it even includes all the cookies and other things the browser would normally send. Link to comment Share on other sites More sharing options...
thescientist Posted October 23, 2013 Share Posted October 23, 2013 (edited) are you sure success is even firing? log some text too. success: function(response) { console.log('success callback run'); console.log(response);} and make sure the console is open before the request is made. Edited October 23, 2013 by thescientist Link to comment Share on other sites More sharing options...
biza Posted October 28, 2013 Author Share Posted October 28, 2013 (edited) So, thescientist, I have done what you said, but I don't have any success, I have the console firebug open before the request is made , and the POST not have been send. Edited October 28, 2013 by biza Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now