dongrandi8411 2014-04-24 07:25
浏览 55

服务器端的JQuery ajax序列化输入文本字段为空

I have a couple of radio fields and an input text type field on my form:

<input name="cbPerson" type="radio" value="1" checked/>Person1
<input name="cbPerson" type="radio" value="2"/>Person2
<input name="txtName" type="text" />

I am serializing my form this way:

$.ajax({
            type: 'post',           
            url: 'form-1-proc.php', 
            data: $('form').serialize(),
            success: function (data) {              
                 alert(data);
            },
            error: function () {
                alert('error');                 
            }
    });

And I am getting the data on server side (form-1-proc.php) this way:

$person = $_POST['cbPerson'];
$name = $_POST['txtName'];

The variable $person has an expected value, but the variable $name never brings a value and it is always "0".

This page is worked as a separate page on Joomla driven website btw, the page itself is on the root of the site so doesn't mess up with Joomla routing and stuff.

Can you please tell me how can I get the data from the input text fields?. Both have a name attribute and on the client it holds a value if i output the value of serialize function thru console or an alert with javascript.

Thank you.

  • 写回答

2条回答 默认 最新

  • doulu4413 2014-04-24 07:36
    关注

    I am not able to replicate the issue.

    test.php

    <?php
    
    if (count($_POST))
    {
        $person = $_POST['cbPerson'];
        $name = $_POST['txtName'];
        exit("Person = $person | Name = $name");
    }
    
    ?>
    
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script type="text/javascript">
    
    function test()
    {
        $.ajax({
        type: 'post',           
        url: 'test.php', 
        data: $('form').serialize(),
        success: function (data) {              
            alert(data);
        },
        error: function () {
            alert('error');                 
        }
        });
    }
    
    </script>
    
    <form>
    <input name="cbPerson" type="radio" value="1" checked/>Person1
    <input name="cbPerson" type="radio" value="2"/>Person2
    <input name="txtName" type="text" />
    <input type="button" onclick="test();" value="Test">
    </form>
    

    enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探