Getting Jquery Post data via PHP

hello,

forms.php


<?php

extract($_GET);
extract($_POST);

?>
<html>
<head>
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">

function do_update(val) {

	$.ajax({
		   type: "POST",
		   url: "forms.php",
		   data: "keyword="+val,
		   success: function(){	
		   		$("#success").show();

			   }		});	

}</script>
</head>
<body>
<form id="form1" name="form1" method="post" >
      <select name="select" id="select" onchange="do_update(this.value);">
       	<option value="yesterday">Yesterday</option>
        <option value="last_week" selected="selected">Last Week</option>
 	     </select>
</form>
<div id="success" style="display:none;">Value:<?php echo $keyword;?></div>
</body>
</html>

the value posted should be shown via $keyword in php but its not

AnthonySterling; Thanks!!! Perfect!!

You’re most welcome.

Here you go, amend as required. :wink:


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
  
  $key = (int)$_POST['key'];
  
  $array = array(
    0 => 'Invalid',
    1 => 'Eggs',
    2 => 'Bacon',
    3 => 'Beans'
  );
  
  echo $array[$key];
  exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.1.0/build/cssreset/reset-min.css" />
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.1.0/build/cssbase/base-min.css" />
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.1.0/build/cssfonts/fonts-min.css" />
    <title>
      Demo
    </title>
  </head>
  <body>
    
    <form action="script.php" method="post">
      <select id="selector" name="key">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
      <input type="submit" value="submit" />
    </form>
    
    <div id="result"></div>
    
    <script type="text/javascript" charset="utf-8">
      //<![CDATA[
      $(document).ready(function(){
        $('#selector').bind('change', function(){
          $.post('script.php', { key: $('#selector').val() }, function(data){
            $('#result').html(data);
          });
        });
      });
      //]]>
    </script>
    
  </body>
</html>