使用JQuery Mobile使用PHP,POST和JSON将数据发送到服务器时出现问题

调查

我已经对这个主题进行了详尽的研究,但是由于讨论的分散性以及每个人似乎都有非常不同的用例,因此似乎找不到答案。

我正在使用JQuery mobile通过$ .ajax()调用将数据发送到PHP登录/注册脚本。看来,我尝试发送的数据永远不会发送到要评估的服务器,而我对此感到茫然。

我正在尝试从此表单发送数据:

<div data-role="content">
    <form id="reg_form" data-ajax="false">
      <div data-role="fieldcontain">
        <label for="reg_email">Email:</label>
        <input type="email" name="reg_email" id="reg_email" value=""  />

        <label for="reg_pass">Password:</label>
        <input type="password" name="reg_pass" id="reg_pass" value=""  />

        <label for="reg_pass_conf">Password:</label>
        <input type="password" name="reg_pass_conf" id="reg_pass_conf" value=""  />

      <h4 id="reg_notification"><?php echo 'Notifications will appear here...';  ?></h4>
      <button data-theme="b" id="reg_submit" type="button">Register!</button>
      </div>
  </form>
</div>

这是由以下javascript触发的:

$(document).on('pageshow', '#reg_page', function() {
    $("#reg_notification").text("page loaded");

    $(document).on('click', '#reg_submit', function(){
        $("#reg_notification").text("button clicked");

        var formDataReg = $("#reg_form").serialize();

        $.ajax({
            type: "POST", // Method of sending data to server
            url: "php_scripts/reg_handle.php", // php script being sent to
            cache: false,  // requested pages won't be cached by server
            data: formDataReg, // data to be sent to server
            dataType: "json", // data type to be received back from server
            success: onRegSuccess, // function to call on success
            error: onError  // function to call on error
        });

        return false;

    });
});


function onRegSuccess(data, status)
{
    alert(data);
    $("#reg_notification").text(data.email + ' ' + data.pass + ' ' + data.pass_conf);   
}

发送到此php脚本的代码:

<?php

if (isset($_POST['formDataReg'])) {
    $reg_email = 'formData is set';
}else{
    $reg_email = 'formData is not set';
}

$formData = json_decode($_POST['formDataReg']); 

$reg_pass = $formData->{'reg_pass'};
$reg_pass_conf = $formData->{'reg_pass_conf'};

$output = array('email' => $reg_email, 'pass' => $reg_pass, 'pass_conf' =>     $reg_pass_conf);
echo json_encode($output);
?>

但是,如前所述,if / else块检测到甚至没有设置$ _POST ['formDataReg']。当我尝试使用它为变量赋值时,它显然没有要赋值的数据,并且我得到了空值。

我使用警报来验证formDataReg确实在将其传递给ajax调用中的服务器之前保存了正确的表单值。它以某种方式在ajax调用中丢失,或者我无法正确访问它。

如果有人能指出我正确的方向,我将不胜感激。

沙里科夫·弗拉迪斯拉夫(Sharikov Vladislav)

这样:

var formDataReg = $("#reg_form").serialize();

您将表格序列化为表格。现在中formDataReg有这样的内容:

[email protected]&reg_pass=yyy&reg_pass_conf=yyy

您必须在您的php文件中解析此查询:

$email = $_POST['reg_email'];
$pass = $_POST['reg_pass'];
$pass_conf = $_POST['reg_pass_conf'];

但是您尝试使用$_POST['formDataReg']未发送的内容。所以这是错误的。是的,您formDataReg的JS中有变量,但这没有任何意义。您已经使用ajax请求发送了序列化的字符串(查询),并且不得不处理它。

所以这段代码:

if (isset($_POST['formDataReg'])) {
    $reg_email = 'formData is set';
}else{
    $reg_email = 'formData is not set';
}

$formData = json_decode($_POST['formDataReg']); 

将无法工作,因为您尚未发送,formDataReg并且此键在$_POST数组中没有任何值

这:

$reg_pass = $formData->{'reg_pass'};
$reg_pass_conf = $formData->{'reg_pass_conf'};

$output = array('email' => $reg_email, 'pass' => $reg_pass, 'pass_conf' =>     $reg_pass_conf);
echo json_encode($output);

应该可以正常工作。

让我知道有什么不清楚的地方。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在单击按钮时使用AJAX将表单序列化的数据和文件发送到服务器

使用post方法时,Volley将空参数发送到服务器

使用Ajax POST请求将图像和JSON数据发送到服务器?

使用POST方法和HttpURLConnection将Android的JSON对象发送到PHP服务器

Volley使用POST方法将JSONObject发送到服务器

无法使用Java将POST请求中的ArrayList发送到REST服务器

使用jquery / ajax和php将数据发送到json文件

使用AT命令将Json数据发送到服务器

使用React JS和Laravel将带有图像文件的POST请求发送到数据库时出现内部服务器错误

使用POST每周将数据发送到服务器

通过使用Axios.post将数据发送到后端,数据格式出现问题

Lotusscript:使用PHP将电子邮件中的文件发送到网站时出现问题

jquery-步骤| 在ajax内容加载时将数据发送到服务器

使用名称值对将Json POST发送到服务器

使用jQuery post或类似的异步JavaScript请求将空数组发送到服务器

使用ajax将JSON发送到操作时,参数为null(返回内部服务器错误500)

jQuery ajax post不将数据发送到服务器端(使用PHP)

使用HTTP Post将机密数据发送到服务器的安全方法

使用Alamofire从PHP服务器检索JSON时出现问题

无法使用godaddy服务器将laravel推送通知发送到ios mobile

angularjs 在使用 POST 将数据发送到服务器时使 json 格式错误

Angular 6 和 PHP:即使在使用附加到 httpParams 时,HttpClient 也不会将数组发送到服务器

定期使用python将Json数据从文件发送到服务器

使用 AsyncTask 和 POST 方法将变量发送到服务器并取回 JSON 对象

无法在带有 Java 的 Android Studio 中使用 HTTP POST 将 JSON 发送到我的服务器

如何使用 AJAX 将 JSON 数据发送到 ASP 服务器?

单击按钮以使用 jQuery 将数据发送到服务器时网页消失

使用 Laravel 和 Guzzle 包将 Web API 发送到 Spotify 时出现问题

使用带有 jQuery 的 POST 请求将数据发送到服务器