![](/img/trans.png)
[英]Submitting an html with php and saving to MySQL Db using Ajax and jQuery?
[英]sending jquery string via AJAX and saving it to html file using PHP
我正在嘗試通過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 );
但這是行不通的。
無需重復兩次相同的代碼,您可以簡單地增加post變量的數量:
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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.