hello friends i wanted to use auto populate text box based on another select box using jquery ajax and wanted to pass values of both the check boxes to a php form and insert in db

my code id :

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquerymin.js" type="text/javascript" ></script>
<script>
function dynamic_Select(ajax_page,username)
{
$.ajax({
type: "POST",
url: ajax_page,
data: "ch=" + username,
dataType: "html",
success: function(html){    
$("#demo").html(html); 
}

  }); }
</script>
</head>

<body>
<form method="post" action="insert.php" id="form1">
 <select name="username"  onchange="dynamic_Select('pages.php',this.value);" id="username"  >
 <option value="1">a</option>
 <option value="2">b</option>
 </select>
 <input type="submit" />
 </form>
<div id="demo">

</div>
</body>
</html>

-----------------------
[B]pages.php[/B]

<?php  
ini_set('display_errors', 1);  
error_reporting(E_ALL);  
  require_once 'connection.php';
  $id=$_POST['ch'];
 
?>
							  
	<input type="text" value="<?php echo $id; ?>"  name="abcd" id="abcd"/> 

--------------------------------
[B]insert.php[/B]

<?php

echo "value is :".$_POST['abcd'];

?>

my problem is result is loaded in resultant div tag but when i try to insert into database or display value in php file it says undefined index abcd

Recommended Answers

All 4 Replies

In your ajax request:
data is always send as GET despite

type: "POST"

so in pages.php use

$id=$_GET['ch'];

even better if you check if there actual is a value

if (isset($_GET['ch'])){
    $id=$_GET['ch'];
    }
else // default or error message
Member Avatar for diafol

Personally, I'd get rid of the inline onchange attribute and have a dedicated handler.

@pzu

Are you sure about the 'post' to 'get' bit? I've always used 'post' and $_POST without any problem. From the jQuery page on .ajax:

type (String)
Default: 'GET'
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

actually i had used same method in some another project it worked without any problem but same thing i am trying its giving error even when i use firebug content is being loaded in the div but when form is submitted it doesnot pass only those values which ajax has returned


just check attachement u can get a clear idea.plz help me out

got it from the jquery website
But haven't tested it

data Object, String
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

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.