2

我的解决方案是使用 onClick 事件调用 PHP 页面、发布参数并使用 AJAX 返回结果。我在使用指定 URL 调用 PHP 页面时插入参数。这是我的Javascript:

function uploadContact() {
        var name, email, phone, badgeid;
        if(window.localStorage.length > 0)
        {
            name = window.localStorage.getItem('name');
            email = window.localStorage.getItem('email');
            phone = window.localStorage.getItem('phone');
        }
        else
        {
            name = $('input[name=fullname]').val();
            email = $('input[name=email]').val();
            phone = $('input[name=phone]').val();
        }
        $.ajax({
             url: 'UpdateContactList.php?name=' + name + '&email=' + email + '&phone=' + phone,
             type: $form.attr('method'),
             dataType: 'json',
             success: function(responseJson) {
             $form.before("<p>"+responseJson.message+"</p>");
        },
        error: function() {
            $form.before("<p>There was an error processing your request.</p>");
        }});
    }

我获取参数的 PHP 代码:

<?php

$response = array();

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
  // if form has been posted process data

  // you dont need the addContact function you jsut need to put it in a new array
  // and it doesnt make sense in this context so jsut do it here
  // then used json_decode and json_decode to read/save your json in
  // saveContact()
   $data = array( 'fullname' => $_POST['name'], 'email' => $_POST['email'], 'phone' => $_POST['phone']);
      //These line is for testing only, remove it when done testing
   $test = $_POST['name'] . ' ' . $_POST['email'];
   echo "<script>alert('$test');</script>";
  // always return true if you save the contact data ok or false if it fails
   $response['status'] = updateContact($data) ? 'success' : 'error';
   $response['message'] = $response['status']
          ? 'Your submission has been saved!'
          : 'There was a problem saving your submission.';

   header("Content-type: application/json");
   echo json_encode($response);
   exit;
}
...
 function updateCacheFile($filename)
{
    $filename = "contact";
    $filename =  $filename . ".appcache";
    $cachefile = fopen ($filename, "a+");
    ....
    file_put_contents($filename, $cache_content);
}

请注意2个问题:

  1. 我可以使用浏览地址链接中的参数直接调用 PHP 页面,这样做不会返回任何内容。正常吗?

  2. 从 JavaScript 调用。测试线(调用警告消息框)不起作用,没有消息出现。但是缓存文件仍然更新,这意味着 PHP 页面被调用并执行诸如写入文件之类的操作。

此 PHP 页面中有 2 个文件要写入。两者都正确写入,但参数不会显示,也不会显示消息框。

请告诉我这里出了什么问题。

P/S:小细节,带空格的参数可以正确传递吗?因为我希望用户输入他们的全名,例如“Bill Jobs”。

感谢 Stackoverflow 社区。

4

3 回答 3

2
   echo "<script>alert('$test');</script>";
  // always return true if you save the contact data ok or false if it fails
   $response['status'] = updateContact($data) ? 'success' : 'error';
   $response['message'] = $response['status']
          ? 'Your submission has been saved!'
          : 'There was a problem saving your submission.';

   header("Content-type: application/json");
   echo json_encode($response);

所有headers 必须在开始echo或以其他方式刷新输出之前执行。

   header("Content-type: application/json");
   echo "<script>alert('$test');</script>";
  // always return true if you save the contact data ok or false if it fails
   $response['status'] = updateContact($data) ? 'success' : 'error';
   $response['message'] = $response['status']
          ? 'Your submission has been saved!'
          : 'There was a problem saving your submission.';

   echo json_encode($response);
于 2012-04-16T04:17:34.120 回答
2
$.ajax({
         url: 'UpdateContactList.php',
         data:{'name':name,'email': email,'phone':phone,'badgeid ':badgeid },
         type: $form.attr('method'),
         dataType: 'json',
         success: function(responseJson) {
         $form.before("<p>"+responseJson.message+"</p>");
    },
    error: function() {
        $form.before("<p>There was an error processing your request.</p>");
    }});
于 2012-04-16T04:18:15.533 回答
1

尝试在 ajax 的数据参数中发送您的信息并将类型设置为发布。像这样...

$.ajax({
         url: 'UpdateContactList.php',
         type: 'post',
         dataType: 'json',
         data: {'key1': value1, 'key2':value2},
         success: function(responseJson) {
         $form.before("<p>"+responseJson.message+"</p>");
    },
于 2012-04-16T04:29:11.560 回答