I have a web page with three links - Auckland, Perth and Wellington and on the same page I have two div’s with id’s of content and images respectively. In my mySQL database called destinations I have a table called information. This has three columns, id, dest_content (text) and dest_image (text) with contains a path to an image such as …/images/auckland.jpg. I would like to be able to click on say the Auckland link and pull the data from the information table related to Auckland which is in the first row of the table (the one with the id of 1) and show the dest_content inside the content div on my webpage and the dest_image in the images div.
I’ll try and post a simpler example:
Here is a file called name.php. I want to send the id of “1” to name1.php and then I want name1.php to send back a message to name.php. I would like name1.php to send back the message ‘The id that has been sent is 1’. The problem is that $output is an undeclared variable according to name.php because name1.php hasn’t been called until I click on the link.
There are lots of examples on the web / books etc where I could send the id to name1.php and it calls on a third page to display the message ‘The id that has been sent is 1’ but I can’t find out how to send information back to the original name.php not some other page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Query String Link Example</title>
</head>
<body>
<p><a href="name.php?id=1">This is sending the id of 1</a></p>
<?php
echo 'The current value of $output is '. $output;
?>
</body>
</html>
and here is the name1.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Query String Link Example</title>
</head>
<body>
<p>
<?php
$id = $_GET['id'];
$output = 'The id that has been sent is, '.$id;
include name.php;
?>
</p>
</body>
</html>
OK I’ve got it sorted - don’t try and use php for both ways of communication!
Here’s what I tried using AJAX and php that seems to fit my requirements that I can then build on.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Query String Link Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("name1.php",
{ id: 1
},
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
$("div").html(data);
});
});
});
</script>
<style type="text/css">
.some_style {
color: #7A6767;
font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
font-style: italic;
font-weight: bold;
font-size: 14px;
}
</style>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
<div class="some_style">
<p>Some existing information</p>
</div>
</body>
</html>
and the name1.php file
<?php
$id = $_POST['id'];
$info1 = 'This is a response from name1.php';
$info2 = 'The id that you sent was '.$id ;
echo "<p>$info1</p>
<p>$info2</p>";
?>