簡體   English   中英

將乘法文本框輸入作為新記錄添加到SQL數據庫中

[英]Add multipe text box inputs into an SQL database as a new record

我正在使用此代碼向表單中添加更多文本框。 基本上,我允許這樣做,以便用戶可以添加他們說的多種語言。

<script>
jQuery(function($) {
    var i = 0;
    $('#theButton').click(addAnotherTextBox);
    function addAnotherTextBox() {
        $("#theForm").append("<br><label>Language <input type='text' name='language" + i + "' >");
    }
});
</script>


<div id='theForm'></div>
<form id="form" name="form">
   <input id='theButton' type='button' value='Add Medical History'>
   <input id="submit" onclick="myFunctionlanguage()" type="button" 
value="Update" class="btn btn-primary btn-block" >
</form>

提交按鈕上的功能基本上使用ajax將收集到的數據發布到使用ajax的PHP文件中(因此,無需定向到另一個頁面或刷新即可發布它。

我目前擁有的PHP代碼是

$language = $_POST['language'];    
if (isset($_POST['language1'])) {
    $sql_insert1 = "INSERT into `languages` (`language`,`cc`,`userid`)
VALUES('$language','cc','$user->id')";
    mysql_query($sql_insert1) or die("Insertion Failed:" . mysql_error());
}

添加新文本框后,這似乎並不會添加我鍵入的每種語言。

有任何想法嗎?

提前致謝。

編輯:-我嘗試了下面的示例,但它似乎仍未添加到我的數據庫中。

可能與我的實際點擊功能有關嗎?

<script>
 function myFunctionlanguage() {
    var language = document.getElementById("location").value;       
    // Returns successful data submission message when the entered information is stored in database.
    var dataString = '&language1=' + language;
    if (location == '') {
        alert("Please enter a language!");
    } else {
        // AJAX code to submit form.
        $.ajax({
            type: "POST",
            url: "includes/updatelanguage.php?userid=<?echo $user->id;?>",
            data: dataString,
            cache: false,
            success: function(html) {
                alert(html);
                document.getElementById('close').click()
                window.location.reload();
            }   
    });
    }
    return false;
}
</script>

嘗試一下。 當我得到您時,它可能對您有用。

<script>
jQuery(function($) {

$('#theButton').click(addAnotherTextBox);
function addAnotherTextBox() {
var i=parseInt($("#max").val());
i=i+1;
$("#max").val(i);
$("#form").prepend("<br><label>Language <input type='text' name='language" + i + "' >");
}
});

</script>

<form id="form" name="form">
<input type="hidden" name='max' id="max" value="0"/>
<input id='theButton' type='button' value='Add Medical History'>
<input id="submit" onclick="myFunctionlanguage()" type="button" 
value="Update" class="btn btn-primary btn-block" ></form>



$max=$_POST['max'];

for($i=1;$i<=$max;$i++){

$language = $_POST['language'.$i];


if (isset($_POST['language'.$i])) {

$sql_insert1 = "INSERT into `languages`
(`language`,`cc`,`userid`)
VALUES('$language','cc','$user->id')";


mysql_query($sql_insert1) or die("Insertion Failed:" . mysql_error());
}

}

與其嘗試為每個輸入框動態分配一個名稱,不如將它們命名為所有languages[] ,這將使您將所有具有名稱languages[]的輸入框作為$POST數組的一個變量發布。

HTML:

<script>
jQuery(function($) {
    $('#theButton').click(addAnotherTextBox);
    function addAnotherTextBox() {
        $("#theForm").append("<br><label>Language <input type='text' name='languages[]' >");
    }
});

</script>

<div id='theForm'></div>
<form id="form" name="form">
<input id='theButton' type='button' value='Add Medical History'>
<input id="submit" onclick="myFunctionlanguage()" type="button" 
value="Update" class="btn btn-primary btn-block" ></form>

PHP:

if (isset($_POST['languages'])) {
    foreach ($_POST['languages'] as $language) {}
        $sql_insert1 = "INSERT into `languages` (`language`,`cc`,`userid`) VALUES ('$language','cc','$user->id')";

        mysql_query($sql_insert1) or die("Insertion Failed:" . mysql_error());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM