如何在MySql中保存DIV的内容?
我有将index.php连接到mydata_post.php的脚本,我在其中调用要保存在MySql中的数据。
现在问题是我在DIV中显示之后尝试保存数据我在MySql表中获取空内容。
如果我使用输入而不是Div来调用该脚本。
我要保存的Div内容位于:
<form name="form1" method="POST" action="mydata_post.php">
<div id="name2" name="name"></div>
<div id="lastname2" name="lastname"></div>
<input type="submit" name="Submit" value="Submit">
</form>
任何人都可以提供帮助,这可能是错误吗?
<!DOCTYPE html>
<html lang="en" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta charset="utf-8" />
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form name="form1" method="POST" action="mydata_post.php">
<div id="name2" name="name"></div>
<div id="lastname2" name="lastname"></div>
<input type="submit" name="Submit" value="Submit">
</form>
<center>
<h1>Authorization step:</h1>
<div id="user-info"></div>
<button id="fb-auth">Please login here</button>
</center>
<div id="result_friends"></div>
<div id="fb-root"></div>
<script>
function sortMethod(a, b) {
var x = a.name.toLowerCase();
var y = b.name.toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
window.fbAsyncInit = function() {
FB.init({ appId: '<?= $sApplicationId ?>',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
function updateButton(response) {
var button = document.getElementById('fb-auth');
if (response.authResponse) { // in case if we are logged in
var userInfo = document.getElementById('user-info');
var myname = document.getElementById('name2');
FB.api('/me', function(response) {
myname.innerHTML = response.name;
userInfo.innerHTML = '<img name="userimage" src="https://graph.facebook.com/' + response.id + '/picture">'+ "<br> Your Name:" + response.name + "<br> Your ID:" + response.id ;
button.innerHTML = '<div id="logout_bt">Logout</div';
});
// get friends
FB.api('/me/friends?limit=<?= $iLimit ?>', function(response) {
var result_holder = document.getElementById('result_friends');
var friend_data = response.data.sort(sortMethod);
var results = '';
for (var i = 0; i < friend_data.length; i++) {
results += '<div><img src="https://graph.facebook.com/' + friend_data[i].id + '/picture">' + friend_data[i].name + "<br> Your ID:" + friend_data[i].id + '</div>';
}
// and display them at our holder element
result_holder.innerHTML = '<h2>Result list of your friends:</h2>' + results;
});
FB.api('/me/friends?limit=<?= $iLimit ?>', function(response) {
var id_holder = document.getElementById('lastname2');
var id_data = response.data.sort(sortMethod);
var id_results = '';
for (var i = 0; i < id_data.length; i++) {
id_results += '<div> ' + id_data[i].id + '</div>';
}
// and display them at our holder element
id_holder.innerHTML = '<h2>Result list of your friends:</h2>' + id_results;
});
button.onclick = function() {
FB.logout(function(response) {
window.location.reload();
});
};
} else { // otherwise - dispay login button
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
window.location.reload();
}
}, {scope:''});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
mydata_post.php - 文件
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name','$lastname','$email')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
?>
答案 0 :(得分:0)
您必须使用输入而不是div才能通过表单传递其内容。这不起作用:
<div id="name2" name="name"></div>
这将:
<input type="text" name="name" value="" />
此外,您在HTML中缺少“电子邮件”输入,而在mydata_post.php中使用它。所以你必须使用这样的东西:
<form name="form1" method="POST" action="mydata_post.php">
<input type="text" id="name2" name="name" value="" />
<input type="text" id="lastname2" name="lastname" value="" />
<input type="text" id="email2" name="email" value="" />
<input type="submit" name="Submit" value="Submit">
</form>
还记得在使用mysql_real_escape_string的SQL请求中使用它之前转义数据。或者切换到PDO或mysqli并使用预准备语句。
答案 1 :(得分:0)
您必须创建一个隐藏的输入:
<input type=hidden name=name />
在POST之前调用submit
的{{1}}属性中的换行form
函数,将onsumbit
中的数据放入隐藏字段。
div
作为旁注,您还需要清理字符串并切换到<form name="form1" method="POST" action="mydata_post.php" onsubmit="moveData();">
<script>
function moveData(){
var data = document.getElementById('divNameID').innerHTML;
var form = document.getElementsByName('form1')[0];
form['name'].value = data;
}
</script>
或PDO
以防止SQL注入。
答案 2 :(得分:0)
按照以下代码
<div id="name">
<span>hi</span>
</div>
<input type="button" onclick="test()" value="click"/>
以下是jquery
<script>
function test(){
var a=$('#name').html();
alert(a);
}
</script>
JSFiddle链接demo
从jquery获取该值,然后将该字符串值分配给textarea。之后获取插入MYSQL的$ _POST [&#39; textarea&#39;]值。