1

有人可以帮我将这些数据发送到一个 .php 页面,我可以在我的 PHP 页面上接收它

javascript:

postToSql(){
var ajax;
if (window.XMLHttpRequest)
 {
          // code for IE7+, Firefox, Chrome, Opera, Safari
 ajax=new XMLHttpRequest();
}
else
{
    // code for IE6, IE5
 ajax=new ActiveXObject("Microsoft.XMLHTTP");
 }
  ajax.onreadystatechange=function()
 {
  if (ajax.readyState==4 && ajax.status==200)
 {
   alert(ajax.responseText); //receiving response
 }
 };

 var name = $("#entry_1274804157").val();
//alert(name);
 var company= $("#entry_1828184698").val();
var phone=$("#entry_2039177352").val();
var email=$("#entry_1545475878").val();
 var comments=$("#entry_1846523632").val();
 var params = {
    "name":name,
    "company":company,
    "phone":phone,
  "email":email,
  "comments": comments
 };     
//var jsonText = JSON.stringify(params);
 ajax.open("POST", "view/templates/includes/insertgoogle.php", false);
 ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 ajax.send("totalJsonStr="+params);
 //alert(totalJsonStr);
 // alert(params);
 return true;
 }
</script>

HTML:

<form action="https://docs.google.com/asgsasdfasg/formResponse"  method="POST" id="" target="_self" onsubmit="return postToSql();">

编辑:这就是我收到它的方式:

 if(isset($_POST['totalJsonStr'])) 
{
   $jsonVal = json_decode($_POST['totalJsonStr']);
$jsonVal2 = json_decode($jsonVal);
var_dump($_POST['totalJsonStr']);
var_dump($jsonVal); 
var_dump($jsonVal2);
$name = $jsonVal2->{'name'};
$company= $jsonVal2->{'name'};
 $phone= $jsonVal2->{'name'};
$email= $jsonVal2->{'name'};
$comments= $jsonVal2->{'name'};
 mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("test") or die(mysql_error());
        mysql_query("INSERT INTO  `testgoogle` ( Name, Company, Phone, Email, Comments ) 
VALUES ('$name','$company', '$phone', '$email', '$comments')");
        Print "Your information has been successfully added to the database.";
    return;
}
    else 
{ 
   die("No Data Found");
} 
4

4 回答 4

0

你在哪里创建“ajax”对象?

var ajax;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    ajax=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
    ajax=new ActiveXObject("Microsoft.XMLHTTP");
  }  

从这里:http ://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp

于 2013-10-17T11:03:06.043 回答
0

如果你想使用 $_GET 那么,

消除:

ajax.open("POST", "view/templates/includes/insertgoogle.php", true);

添加:

ajax.open("GET", "view/templates/includes/insertgoogle.php", true);

有用的链接: http: //www.degraeve.com/reference/simple-ajax-example.php

于 2013-10-17T10:58:34.603 回答
0

由于没有什么对我有用,我最终使用了有效的 jquery ajax。

于 2013-11-11T11:21:58.883 回答
0

使用POST方法,

完美的解决方案是将所有值存储在一个数组中,并使用json.stringify().
在 php 中,json_decode()用于解码您的 json 字符串。

更新

将此添加到您的javascript中,

<script type="text/javascript">

function postToSql(){
    var ajax;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      ajax=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
     ajax=new ActiveXObject("Microsoft.XMLHTTP");
      }
    ajax.onreadystatechange=function()
      {
      if (ajax.readyState==4 && ajax.status==200)
        {
           alert(ajax.responseText); //receiving response
        }
      }
        var name = "1234";
        var company= "1234";
        var phone="1234";
        var params = {
            "name":name,
            "company":company,
            "phone":phone,
        };      
        var jsonText = JSON.stringify(params);
        ajax.open("POST", "view/templates/includes/insertgoogle.php", true);
        ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        ajax.send("totalJsonStr="+jsonText);
}
</script>
<form action="https://docs.google.com/asgsasdfasg/formResponse"  method="POST" id="" target="_self" onsubmit="postToSql();return false;">

将此添加到 php

<?php
        if(isset($_POST["totalJsonStr"])) 
  {
        $jsonVal = json_decode($_POST["totalJsonStr"]);
        print $jsonVal->{'name'}; 
        print $jsonVal->{'company'};
        print $jsonVal->{'phone'};
  }
        else 
  { 
       die("No Data Found");
  } 
    ?>
于 2013-10-17T11:29:21.070 回答