我的html页面动态创建textareas。我正在尝试使用Javascript,Ajax和PHP从这些文本区域将数据从页面输入mysql。创建时,每个文本区域都有一个唯一的ID。 #num按钮用于将文本输入数据库。这是我的html代码段:



<form method="POST">
		<div id ="textFields" class="form-group">

            <!--align it all in the center-->
            <p align="center">

            <!--small break-->
            <br>


            <!--textarea-->
            <textarea id="note0" input type="text" name="content" placeholder="Enter note, max limit 200 words" class="form-control" rows="5"  style="width: 80%;overflow:auto;"></textarea>



                <!--end of p alignment-->
                </p>

        <!--End of textFields div-->
        </div>

        <!--div for buttons-->
        <div align="left">
            <button id="num" class="w3-button w3-large w3-circle w3-red w3-card-4"  onclick="return false">+</button>
        </div>
		</form>





单击#num按钮后,将调用Javascript函数,该函数与Ajax一起使用,以使页面不会刷新并在单击#num按钮时保持不变。此函数应该(或我希望)将值传递到php页面,并在数据输入数据库后取回控件。



$(document).ready(function(){
    var note_id = 0;


	//on the click of the submit button
	$("#num").click(function(){

		//get the form values
		var inputField = $('<p align="center"><br><textarea id="note' + note_id + '" input type="text" name="content" placeholder="Enter note, max limit 200 words" class="form-control" rows="5" style="width: 80%;overflow:auto;"></textarea></p>');

		$.ajax({
			type: "POST",
			url : "Savenotes.php",
			data : {
				content: $('#note'+note_id).serialize()
			 },
			success: function(response)
			{
				alert(response);

			}
		});

		note_id++;
	});
});





这是将文本区域内容存储到数据库中的Savenotes.php文件:



$Content=$_POST['content'];

$sql_users="INSERT INTO notes(Contentdb) VALUES ('$Content')";
if (mysqli_query($connect,$sql_users))
{
	echo ('Note saved into the database.');
}
else
{
	echo ('Note could not be saved, Please try again!');
	header('Location:finalAppend.html');

}
?>





单击#num按钮后,我无法在数据库中获得与用户在网页上的文本区域中输入的数据相同的数据。每个条目仅显示“ content =”。我如何获得用户输入的正确值?

最佳答案

$.ajax({
        type: "POST",
        url : "Savenotes.php",
        data : {
            content: $('#note'+note_id).val().serialize()
         },
        success: function(response)
        {
            alert(response);

        }
    });

关于javascript - 尝试在不使用php和ajax刷新页面的情况下将数据输入mysql数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43818174/

10-10 23:44
查看更多