Hello,

I have a PHP page called summary.php and I m trying to make the jquery script to post piece of data when <select> is modified. This is my code atm

<script>
    $("select").change(function () {
		page_id=$(this).attr('id');
		alert(page_id);
		$.post("#", {id:page_id});
        })
        .change();
</script>

<?php if(isset($_POST['id'])){
	die("YES!");
}

echo $_POST['id'];
?>

The thing is that it will not set the $_POST variable, although theoretically it should ? Atleast according to my logic. Help me please, I m going insane with this.

Are you sure that posting to '#' is supposed to work ? Shouldn't that be window.location.href ?

I had 'summary.php' there before and that didnt work, so I changed it to '#'. And no, I m not sure if this should work. I m pretty noob with jquery

Well. You will never see the die, because your script runs on the server, and you will never "see" the die message. That message is returned in the post callback, which you have not yet created. I suggest you read up on how AJAX calls work.

Basically, such a call requires a function that handles the outputted result from your summary PHP script. Whether it outputs HTML, or JSON data, the callback function should do something with it. You may want to start with something like this:

<script>
    $("select").change(function () {
	page_id = $(this).attr('id');
	alert(page_id);
	$.post("summary.php", {id: page_id}, function(data){
            alert(data);
        });
    }).change();
</script>
<?php 
  if (isset($_POST['id'])) {
	echo "YES: " . $_POST['id'];
  }
  else {
    echo "NO";
  }
?>

I still don't quite get it. If I run it this way then the $_POST will never ever be set and it will always echo "NO". I see that the id variable is set so why isn't it posting it to the summary page ? And if its not really sending it then why am I not getting an error ? The callback you suggested, I dont really need it. I need the script to change the value in database when the selection has been changed. I dont need to receive any data back from the page/script. I just dont get it, it seems so easy and when I look at the synax at jquery web page then I m doing everything correct. I m completely lost here.

Okay, now that what you want is more clear (to me), I'll try to make a demo tomorrow.

I tested the following on my server, and the column is incremented as expected.

<?php
  if (isset($_POST['id']))
  {
    $id = $_POST['id'];
    $query = "UPDATE `database`.`table` SET `count` = `count` + 1 WHERE `id` = $id";
    @mysql_connect('localhost', 'user', 'password');
    @mysql_query($query);
  }
  else
  {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>dw-js-412992</title>
  <meta http-equiv="content-language" content="en" />
  <meta name="robots" content="noindex,nofollow" />
</head>
<body>
  <select id="1">
    <option>1</option>
    <option>2</option>
  </select>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $("select").change(function(){
        var page_id = $(this).attr('id');
        $.post("index.php", { id: page_id });
      }).change();
    }); 
  </script>
</body>
</html>
<?php
  }
?>
commented: Very nice post ! Thanks for the effort +2

It works perfectly !!! Thank you so much. I discovered that maybe it worked perfect before too. The only thing is that for some reason it wont work with 'echo' meaning it doesnt echo the value even though condition evaluated true. I had trouble with ur code cause I modified the php part to echo when true and it didnt work. So i decided to create dummy table and test it with ur EXACT code and it worked. Thanks alot man !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.