I'm new to ajax and I'm stuck in a problem. I want to post the id of the clicked element in a result list to another page (in localhost). When I want to get the posted id from the ajax data I get undefined index id (which is $id = $_POST['id']). how can I solve this ?
I appreciate any help or advice.
index.php
<section id= "searchbar">
<form method="get" action="index.php" name="myForm" id="myForm">
<h3>Search</h3>
<table>
<tr>
<td><input id="wilaya" type="text" name="wilaya" class="resizedTextbox" placeholder="Choose your Wilaya"></td>
<td><input id="surface" type="text" name="surface" class="resizedTextbox" placeholder="Choose the surface"></td>
<td><input id="search" type="submit" class="resizedTextbox" value="Search"></td>
</tr>
</table>
</form>
</section>
<section id="resultlist">
<table width="100%" height="90%" id="table1">
<thead>
<tr>
</tr>
</thead>
<?php
$array[] = null;
$i = 1;
while ($results = mysql_fetch_assoc($result)) {
?>
<tr>
<div class="round-button">
<!--echo '<img name= "house" class= "imagehouse" src="images/' . $results['photo'] . '" id=' . $results['id'] . ''-->
<?php echo "<img src= images/" . $results['photo'] . " width='50' id=" . $results['id'] . " />"; ?>
<td id="nametable" width="10%"> <?php echo $results['name']; ?></td>
<td id="wilayatable"width="10%"><?php echo $results['wilaya']; ?></td>
<td width="10%"><?php echo $results['surface'] . " m²"; ?></td>
</tr>
<?php
$i++;
}
}
?>
</table>
</section>
<?php
//extract data from the post
//set POST variables
$url = 'http://localhost/testcurl.php';
$fields = array(
'wilaya' => urlencode($_POST['wilaya']),
'id' => urlencode($_POST['id']),
);
//url-ify the data for the POST
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
<?php
var_dump($_POST['id']);
?>
and that's the function that get the id of the clicked image
$('img').click(function () {
alert(this.id);
var id = this.id;
$.ajax({
url: 'index.php',
type: 'post',
data: {
'id': id
},
success: function (msg) {
alert('Success!');
}
});
$.get("http://localhost/testcurl.php");
window.location = 'http://localhost/testcurl.php';
});
testcurl.php
here I send a post the id to the server
<?php
$url = "http://localhost/testcurlserver.php";// where you want to post data
$wilaya = isset($_POST['wilaya']);
$id = isset($_POST['id']);
$fields = array(
'wilaya' => urlencode($_POST['wilaya']),
'id' => urlencode($_POST['id']),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
var_dump($output); // show output
?>
testcurlserver.php here is the server code
<?php
if (isset($_POST['id'])) {
$jj =$_POST['id'];
var_dump($jj);
echo "it worked";
$wilaya = $_POST['wilaya'];
// connect to database
$db = mysql_connect('localhost', 'root', 'test123') or die('error because:' . mysql_error());
$mydb = mysql_select_db("top360");
$sql = "SELECT id,photo,name, wilaya, surface FROM HOUSE WHERE wilaya= '" . $wilaya . "'";
//-run the query against the mysql query function
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo $row['name'];
}
if ($result === FALSE) {
die(mysql_error());
}
$results = mysql_fetch_assoc($result);
echo $results['name'];
}
here is what I get:
Notice: Undefined index: id in C:\xampp\htdocs\index.php on line 122
Notice: Undefined variable: fields_string in C:\xampp\htdocs\index.php on line 127
Notice: Undefined variable: fields_string in
C:\xampp\htdocs\testcurl.php on line 14 string(0) "" it worked Notice:
Undefined variable: output in C:\xampp\htdocs\testcurl.php on line 31
NULL
Notice: Undefined index: id in C:\xampp\htdocs\index.php on line
147 NULL