我正在尝试通过AJAX将字符串发送到PHP文件。我有两个彩票轮,每个都有一个结果(如果按下"旋转")。我希望收到结果并将其打印到HTML文件中。请看这里: http://zeevm.co.il/rollet/
这是发送第一轮结果的AJAX代码:
function sendwinnertophp(){
var winner = $("#winner").html();
$.ajax({
type: "POST",
url: "getwinner.php",
data: {
"winner": winner
},
cache: false,
success: function(data){
// alert(data);
},
error: function(err){
alert(err);
}
});
}
这是将其保存到HTML文件的PHP代码:
$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];
//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner<br/><hr/>" );
fclose( $file );
但是,我有两个轮子,每个轮子都有自己的结果。我希望复制Ajax代码,以便它发送第二轮的结果并使用PHP将其打印到HTML文件。
我尝试在第一个函数之后添加它:
function sendwinnertophp222(){
var winner222 = $("#winner222").html();
$.ajax({
type: "POST",
url: "getwinner.php",
data: {
"winner222": winner222
},
cache: false,
success: function(data){
// alert(data);
},
error: function(err){
alert(err);
}
});
}
这是PHP文件:
$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];
$winner222 = $_POST['winner222'];
//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner<br/> $winner222<br/><hr/>" );
fclose( $file );
但它不起作用。
答案 0 :(得分:2)
不是两次复制相同的代码,而是简单地增加后期变量的数量:
function sendwinnertophp(){
var winner = $("#winner").html();
var winner_two = $("#winner222").html();
$.ajax({
type: "POST",
url: "getwinner.php",
data: {
"winner": winner,
"winner_two" : winner_two
},
cache: false,
success: function(data){
// alert(data);
},
error: function(err){
alert(err);
}
});
}
在PHP文件中:
$file = fopen("log.html", "a");
$ip=$_SERVER['REMOTE_ADDR'];
$winner = $_POST['winner'];
$winner_two = $_POST['winner_two'];
//write the data
$time = date("H:i dS F");
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner<br/><hr/>" );
fwrite($file, "<b>Time:</b> $time<br/><b>ip:</b> $ip<br/><b>score:</b> $winner_two<br/><hr/>" );
fclose( $file );