Jump to content
Sign in to follow this  
biza

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

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>

Share this post


Link to post
Share on other sites

The data property contains the data that will be sent to PHP:

data: {ref : ref},

This data is received in PHP as $_POST['ref']

Share this post


Link to post
Share on other sites

Try print_r($_POST) to see if anything got there at all.

Share this post


Link to post
Share on other sites

When you use jQuery to send an ajax request, are you sure the method is called "type" instead of "method"?

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by biza

Share this post


Link to post
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); }

Share this post


Link to post
Share on other sites

console.log doesn't return a value. Forget alert(), console.log will show the value in Firebug's console.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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 by biza

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...