gpt4 book ai didi

javascript - 如何将DIV内容保存到MySql

转载 作者:行者123 更新时间:2023-11-30 17:42:28 25 4
gpt4 key购买 nike

如何在 MySql 中保存 DIV 的内容?

我有连接 index.php 和 mydata_post.php 的脚本,我在其中调用要保存在 MySql 中的数据。

现在的问题是,在 DIV 中显示数据后,我尝试将数据保存到哪里,但 MySql 表中的内容为空。

如果我从 Input 而不是 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";
}

?>

最佳答案

您必须使用输入,而不是 div,以便通过表单传递它们的内容。这行不通:

<div id="name2" name="name"></div>

这将:

<input type="text" name="name" value="" />

另外,在 mydata_post.php 中使用它时,您在 HTML 中缺少“电子邮件”输入。所以你必须使用这样的东西:

<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 并使用准备好的语句。

关于javascript - 如何将DIV内容保存到MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com