为什么PHP不读取我的$ _POST数据?

时间:2013-05-01 04:31:46

标签: php javascript jquery mysql ajax

我在JavaScript中有以下功能:

function AddTextBox(id) {
    if (id == 'national age gender' || id == 'arizona age gender') {
    document.getElementById('text_boxes').innerHTML='Age Group: <input name="age_gender" id="age_group" type="text"> <br /> Male population: <input name="age_gender" id="male" type="text"> Female population: <input name="age_gender" id="female" type="text"> <br /> Male %: <input name="age_gender" id="male_percent" type="text">  Female %: <input name="age_gender" id="female_percent" type="text"> <br /> Males per 100 females <input name="age_gender" id="m_per_100_f" type="text"> <br /> <button onclick="validateAgeGender(id);">Add</button>'; 

    }
    if (id == 'national race' || id == 'arizona race') {
    document.getElementById('text_boxes').innerHTML='Category: <input id="cat" type="text"> <br /> White population: <input name="race" id="white" type="text"> White %: <input name="race" id="white percent" type="text"> <br /> Black population: <input name="race" id="black" type="text"> Black %: <input name="race" id="black_percent" type="text"> <br /> Native American population: <input name="race" id="native_american" type="text"> Native American %: <input name="race" id="native_american_percent" type="text"> <br /> Asian: <input name="race" id="asian" type="text"> Asian %: <input name="race" id="asian_percent" type="text"> <br /> Pacific Islander: <input name="race" id="pacific_islander" type="text"> Pacific Islander %: <input name="race" id="pacific_islander_percent" type="text"> <br /> Other: <input name="race" id="other" type="text"> Other %: <input name="race" id="other_percent" type="text"> <br /> <button onclick="validateRace(id);">Add</button>';

    }

}



function validateAgeGender(id) {
    if (id == 'national age gender') {
        var data = $('#age_gender').serialize();
        $.post('/CensusDatabase/database_scripts/NationalAgeGender.php', data);
    }
    if (id == 'arizona age gender') {
        var data = $('#age_gender').serialize();
        $.post('/CensusDatabase/database_scripts/ArizonaAgeGender.php', data);
    }
}

我有以下PHP代码:

if(isset($_POST['age_gender'])) { 
    $formSerialized = $_POST['age_gender'];
    $formData = array();
    parse_str($formSerialized, $formData);
    addRow($formData, $link); 
}

function addRow($dataArray, $link) {
    $age_group = $dataArray[0];
    $populations = array(intval($dataArray[1]) + intval($dataArray[2]), intval($dataArray[1]), intval($dataArray[2]));
    $percents = array(doubleval($dataArray[3]) + doubleval($dataArray[4]), doubleval($dataArray[3]), doubleval($dataArray[4]));
    $m_per_100_f = doubleval($dataArray[6]);
    $query = "INSERT INTO national_age_gender_demographics (age_group, both_pop, male_pop, female_pop, both_percent, male_percent, female_percent, males_per_100_females)
    VALUES ('$age_group','$populations[0]','$populations[1]','$populations[2]','$percents[0]','$percents[1]','$percents[2]','$m_per_100_f')";
    $result = mysqli_query($link,$query);

    if(!$result)  die( "Query: " . $query . "\nError:" . mysql_error() );

}

有谁能告诉我为什么这不起作用?我希望能够通过用户数据添加到表中。

2 个答案:

答案 0 :(得分:1)

如果您使用mysqli,则将mysql_error()更改为mysqli_error()以查找问题

答案 1 :(得分:0)

看来,您没有为表单输入字段设置正确的name属性,您刚刚使用了不同的id属性,但输入字段的名称属性相同。

由于Jquery序列化使用表单元素名称属性和值来将表单元素数据编码为字符串(name = value)以供提交。

如果我错了,请纠正我。