Jump to content

pass values ​​from ajax to php without refresh the page


biza

Recommended Posts

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

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

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

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

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

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

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 by thescientist
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...