0

我在 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() );

}

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

4

2 回答 2

1

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

于 2013-05-01T04:58:17.983 回答
0

It seems,you have not set proper name attribute to form input fields,you have just used different id attributes but same name attributes for input fields.

As Jquery serialize use form elements name attributes and value to encode form elements data as a string (name=value)for submission.

correct me if i am wrong??.

于 2013-05-01T05:21:05.663 回答