简体   繁体   English

如何通过ajax调用将数据从2个不同的页面发送到php?

[英]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 网站1

<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将读取site1的名称和电子邮件,并将其包含在ajax调用中

Site2 Javascript 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? 有没有办法为Ajax调用做类似的事情? Thanks! 谢谢!

Sure. 当然。 in site2, just capture the POST vars, then ask PHP to print their values wherever you need it 在site2中,只需捕获POST变量,然后要求PHP在需要的地方打印它们的值

site2.php 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函数所在的位置,您可以重新进入PHP模式以输出值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM