【发布时间】:2017-04-28 23:19:13
【问题描述】:
我正在构建一个简化的推荐系统。
$(document).ready( function() {
document.getElementById("getref").onclick = function setref() {
$.ajax({
url: 'newref.php', // the script to call to get data
type: "POST",
dataType: 'json', // data format
data: {ip:<?php echo $_SERVER['REMOTE_ADDR'] ?>},
// you can insert url arguments here to pass to api.php
// for example "id=5&parent=6"
success: function(data) // on receive of reply
{
alert('returned ' + data); //get id
//--------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------
$('#bottom_msg').html("<b>freegoldtrial.com/"+id+"</b>");
// Set output element html
// http://api.jquery.com/category/selectors/
}
});
}
});
newref.php 内部
$ip = $_POST['ip'];
$key = substr(md5(microtime()),rand(0,26),5);
include DB.php
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $conn);
$tbl_name = "refs";
$sql="INSERT INTO refs(ip, id) VALUES ('".$ip."','".$key."')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
如何将生成的 $key 变量返回到索引?如您所见,我正在尝试用它替换 div 内容。感谢所有帮助:)
【问题讨论】:
-
echo $key..? -
请stop using
mysql_*functions。 These extensions 已在 PHP 7 中删除。了解prepared 的 PDO 和 @987654328 语句@ 并考虑使用 PDO,it's really pretty easy。