简体   繁体   English

使用Ajax获取表单数据

[英]Get form data using Ajax

I want to get form data using ajax but I can't get the values. 我想使用ajax获取表单数据,但无法获取值。

I have this javascript code to call the ajax function : 我有这个JavaScript代码来调用ajax函数:

$( "#form_scenario_eco" ).submit(function( event ) {
        event.preventDefault(); //Pour éviter que la page se reload
        var formData = ($('#form_scenario_eco').serializeArray());
        $.post("/kohana-v3.3.5/ajax/nouveau_scenario_eco",{scenario_eco : formData}, function(data){
            alert(data);
        }, 'json');
    });

In my ajax file, I get the form data with : 在我的ajax文件中,我使用以下命令获取表单数据:

 $scenario = $_POST['scenario_eco'];

With Chrome, I have an error 500 (Internal Server Error) because I can't get the $_POST['scenario_eco'] . 使用Chrome时,出现错误500(内部服务器错误),因为我无法获取$_POST['scenario_eco']

Can someone tell me how should I get these data please ? 有人可以告诉我如何获取这些数据吗?

You need to figure out the cause of your 500 (Internal Server Error) by checking your error logs. 您需要通过检查错误日志来找出500(内部服务器错误)的原因。 You won't be able to retrieve the form data until the error is fixed. 在纠正错误之前,您将无法检索表单数据。

Here is an example 这是一个例子

HTML 的HTML

<form id="myform">
    <input type="text" name="username"/>
    <input type="password" name="password"/>
    <input type="submit" value="Submit"/>
</form>

JS JS

$("#myform").submit(function(e){
        e.preventDefault();
        $.post("form.php",{ data: $(this).serializeArray() }).done(function(data){
            alert(data);
        })
    });

PHP 的PHP

var_dump($_POST['data']);

Be sure that your inputs marked with 'name' attribute 确保您的输入标有“名称”属性

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

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