ygongdev ygongdev - 4 months ago 5
jQuery Question

How to send data from 2 different page to a php via an ajax call together?

So I need the user to fill a form and then I want to pass that data to another page like so

Site1

<form action="site2.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit">
</form>


Site2 will read the name and email from site1 and include it inside a ajax call

Site2 Javascript

function addMarker(location, map) {
// Add the marker at the clicked location
var marker = new google.maps.Marker({
position: location,
map: map
});

$.ajax({
url: 'insertToDatabase.php',
type: 'POST',
data: { lat: location.lat(),
long : location.lng(),
name : $_POST['name'], // something like this
email: $_POST['email'] // something like this
}
});
}


Is there a way to do something like this for the Ajax call?
Thanks!

Answer

Sure. in site2, just capture the POST vars, then ask PHP to print their values wherever you need it

site2.php

<?php
//capture the POST vars
$name = $_POST['name'];
$email = $_POST['email'];
?>
...HTML & JS code GOES HERE....

Then later on down the page, where your ajax function is, you can re-enter PHP mode to output the values:

$.ajax({
    url: 'insertToDatabase.php',
    type: 'POST',
    data: { lat: location.lat(),
    long : location.lng(),
    name : '<?= $name ?>',
    email: '<?= $email ?>'
    ...
Comments