使用jQuery POST和php序列化并提交表单 [英] serializing and submitting a form with jQuery POST and php

查看:166
本文介绍了使用jQuery POST和php序列化并提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery发送表单的数据。但是,数据不会到达服务器。你能告诉我我做错了什么吗?

  // FORM 

< input type =textname =numesize =40placeholder =Nume>
< input type =textname =telefonsize =40placeholder =Telefon>
< input type =textname =emailsize =40placeholder =Email>
< textarea name =comentariicols =36rows =5placeholder =Message> < / textarea的>
< input id =submitBtntype =submitname =submitvalue =Trimite>
< / form>

Javascript:

  Javascript代码与表单相同:
< script type =text / javascript>
$(document).ready(function(e){

$(#contactForm)。submit(function(){
$ .post(getcontact.php ,$(#contactForm)。serialize())//序列化看起来很好name = textInNameInput&& telefon = textInPhoneInput --- etc
.done(function(data){
if data.trim()。length> 0)
{
$(#sent)。text(Error);
}
else {
$(#sent)。text(Success);
}
});
return false;
})
});
< / script>

和服务器端:

  /getcontact.php 

$ nume = $ _ REQUEST [nume]; // $ nume不包含任何数据。还试过$ _POST
$ email = $ _ REQUEST [email];
$ telefon = $ _ REQUEST [telefon];
$ comentarii = $ _ REQUEST [comentarii];

您可以告诉我我做错了什么吗?

编辑:
检查 var_dump($ _ POST)并返回一个空数组。


奇怪的是,在我的本地机器上测试的相同代码工作正常。
如果我上传我的主机空间上的文件,它停止工作。

我尝试做一个老式的形式,而不使用jQuery,所有的数据是正确的。



我不明白这将是一个服务器配置问题。任何想法?



谢谢!

解决方案

函数

  var datastring = $(#contactForm)。serialize(); 
$ .ajax({
type:POST,
url:your url.php,
data:datastring,
dataType:json,
success:function(data){
// var obj = jQuery.parseJSON(data);如果未指定dataType作为json,则取消注释此
//执行任何您想要的操作服务器响应
},
错误:function(){
alert('error hereing');
}
});

返回类型是json



编辑:我使用 event.preventDefault 来阻止浏览器在这种情况下被提交。



在答案中添加更多数据。
$ b dataType:jsonp如果是跨域调用的话。



beforeSend: //这是一个预先请求的回调函数
$ b $ p $ 完成: //一个函数在请求结束后调用。不管成功或错误都必须执行的代码可以在这里

async: //默认情况下,所有请求都是异步发送的。



cache: //默认为true。如果设置为false,它将强制请求的页面不被浏览器缓存。



查找官方页面这里


i'm trying to send a form's data using jQuery. However, data does not reach the server. Can you please tell me what i'm doing wrong?

 // FORM

   <form id="contactForm" name="contactForm" method="post">
   <input type="text" name="nume" size="40" placeholder="Nume">
    <input type="text" name="telefon" size="40" placeholder="Telefon">
    <input type="text" name="email" size="40" placeholder="Email">
    <textarea name="comentarii" cols="36" rows="5" placeholder="Message">        </textarea> 
   <input id="submitBtn" type="submit" name="submit" value="Trimite">
   </form>  

Javascript:

    Javascript Code in the same page as the form: 
   <script type="text/javascript">
$(document).ready(function(e) {

    $("#contactForm").submit(function() {
        $.post("getcontact.php", $("#contactForm").serialize()) //Serialize looks good name=textInNameInput&&telefon=textInPhoneInput---etc
        .done(function(data) {
            if (data.trim().length >0)
            {
                $("#sent").text("Error");   
            }
            else {
            $("#sent").text("Success");
            }
        });
        return false;
    })
});
 </script>

And server-side:

   /getcontact.php 

     $nume=$_REQUEST["nume"]; // $nume contains no data. Also tried $_POST
     $email=$_REQUEST["email"];
     $telefon=$_REQUEST["telefon"];
     $comentarii=$_REQUEST["comentarii"];

Can you please tell me what am i doing wrong?

EDIT: Checked var_dump($_POST) and it returned an empty array.

The weird thing is that the same code tested on my local machine works fine. If i upload the files on my hosting space it stops working.
I tried doing an old-fashioned form without using jquery and all data is correct.

I don't see how this would be a server configuration problem. Any ideas?

thank you!

解决方案

You can use this function

var datastring = $("#contactForm").serialize();
$.ajax({
    type: "POST",
    url: "your url.php",
    data: datastring,
    dataType: "json",
    success: function(data) {
        //var obj = jQuery.parseJSON(data); if the dataType is not specified as json uncomment this
        // do what ever you want with the server response
    },
    error: function() {
        alert('error handing here');
    }
});

return type is json

EDIT: I use event.preventDefault to prevent the browser getting submitted in such scenarios.

Adding more data to the answer.

dataType: "jsonp" if it is a cross-domain call.

beforeSend: // this is a pre-request call back function

complete: // a function to be called after the request ends.so code that has to be executed regardless of success or error can go here

async: // by default, all requests are sent asynchronously

cache: // by default true. If set to false, it will force requested pages not to be cached by the browser.

Find the official page here

这篇关于使用jQuery POST和php序列化并提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆