发送数据到MySQL的AJAX +的jQuery + PHP [英] send data to MySQL with AJAX + jQuery + PHP
问题描述
我一直在寻找一整天找到一种方法插入一些数据到我的数据库,然后显示在我的网页中已经存在的数据列表中的新数据。
我知道的一些观点我不能做的可能是基础知识,但我刚开始学习JavaScript / AJAX,所以我有一个有限的知识。
我觉得这份工作的70%,完成后,我可以显示使用AJAX / jQuery的/ PHP我的网页数据,但我不明白如何从文本区域再次使用AJAX将数据发送到我的数据库/ jQuery的/ PHP。
到目前为止,这是我做了什么:
的index.php
在这个页面,我想,我需要把一个onClick:函数()上的按钮,但我不知道往哪里放的功能,它应该完全做
<!DOCTYPE HTML>
< HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/>
<冠军> TP2< /标题>
<脚本SRC =jQuery的-1.10.2.min.js>< / SCRIPT>
< /头>
<身体GT;
&所述;脚本的src =javascript.js>&所述; /脚本>
消息:<输入类型=文本ID =消息内容>
<输入类型=按钮ID =信息提交值=Envoyer>
< DIV ID =输出ALIGN =中心>
< / DIV>
< /身体GT;
< / HTML>
javascript.js
该页面包含了AJAX code,它显示我的数据在#output股利。
我只是不知道放什么在数据:{}。所以我的textarea的内容发送到我的PHP文件
$。阿贾克斯({
网址:messages.php
键入:POST,
异步:真正的,
数据:{发生的事情吗? },
数据类型:HTML,
成功:功能(数据){
$('#输出)HTML(数据);
},
});
messages.php
我知道INSERT查询会在这里,但我想不出如何从我的textarea的POST获得的价值。
< PHP
$主机=localhost的;
$用户=根;
$传球=根;
$ DATABASENAME =myDataBaseName;
$ tableName值=意见;
$ CON =的mysql_connect($主机,$用户,$通行证);
$ DBS = mysql_select_db($数据库名称,$ CON);
$结果= mysql_query(SELECT * FROM $ tableName值排序按日期降序极限0,10);
$数据=阵列();
而($行= mysql_fetch_array($结果))
{
回声$行[信息] - $行['日期'。;。
回声< BR />中;
}
?>
您可以使用一个onclick,或使用jQuery的。点击功能按钮...
你把这个脚本标记,通常是在你的头上或内部的document.ready
$(#邮件提交)。点击(函数(){
//在这里我们可以做阿贾克斯之后验证字段不为空。
如果($(#消息内容)。VAL()!=){
$阿贾克斯({
网址:messages.php
键入:POST,
异步:真正的,
数据:{消息:$(#消息内容)VAL()} //你的表单数据后放在这里作为一个JSON对象
数据类型:HTML,
成功:功能(数据){
$('#输出)HTML(数据);
},
});
} 其他 {
//通知他们需要输入数据的用户
}
});
有关messages.php,你的AJAX POST的值现在将提供给你的PHP中的变量$ _ POST ['消息']
$ SQL =INSERT INTO tableName值(消息)VALUES('$ _ POST ['消息'。');
I've been looking all day to find a way to insert some data into my database and then show the new data in the list of already existent data on my webpage.
I know that some of the points I can't get done may be basics but I just started learning Javascript/ AJAX so I have a limited knowledge.
I think that 70% of the job is done, I can display the data on my webpage using AJAX/jQuery/PHP but I can't figure how to send data from a "textarea" to my database using again AJAX/jQuery/PHP.
So far this is what I've done :
index.php
In this page, I figure that I need to put an onClick: function() on the button but I don't know where to put that function and what it should exactly do.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TP2</title>
<script src="jquery-1.10.2.min.js"></script>
</head>
<body>
<script src="javascript.js"></script>
Message: <input type="text" id="message-content">
<input type="button" id="message-submit" value="Envoyer">
<div id="output" align="center">
</div>
</body>
</html>
javascript.js
This page contain the AJAX code that display my data in the #output div.
I just don't know what to put in "data: {}" so the content of my textarea is sent to my php file.
$.ajax({
url: "messages.php",
type: "POST",
async: true,
data: { WHAT GOES HERE ? },
dataType: "html",
success: function(data) {
$('#output').html(data);
},
});
messages.php
I know that the INSERT query will go here but I can't figure how to get the value from the POST of my textarea.
<?php
$host = "localhost";
$user = "root";
$pass = "root";
$databaseName = "myDataBaseName";
$tableName = "comments";
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
$result = mysql_query("SELECT * FROM $tableName ORDER BY date DESC LIMIT 0 , 10 ");
$data = array();
while ($row = mysql_fetch_array($result))
{
echo $row['message']." - ".$row['date'];
echo "<br />";
}
?>
you can use an onclick, or use a jQuery .click function for the button...
you would put this in script tags, usually in your head or inside the document.ready
$("#message-submit").click(function() {
//in here we can do the ajax after validating the field isn't empty.
if($("#message-content").val()!="") {
$.ajax({
url: "messages.php",
type: "POST",
async: true,
data: { message:$("#message-content").val() }, //your form data to post goes here as a json object
dataType: "html",
success: function(data) {
$('#output').html(data);
},
});
} else {
//notify the user they need to enter data
}
});
for messages.php, the value of your AJAX POST will now be available to your PHP in the variable $_POST['message']
$sql = "INSERT INTO tableName (messages) VALUES ('".$_POST['messages']."')";
这篇关于发送数据到MySQL的AJAX +的jQuery + PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!