当用户单击jQuery中的按钮时,我希望运行另一个页面的php脚本。我已经尝试过了,但是没有用。
下面是我的脚本:
要从索引页面单击的按钮
<button id="updatebutton" class="btn btn-sm btn-info" onclick="update_btn_click('params');"><span class="fa fa-refresh"> Generate Cards</span></button>
PHP页面(card_generator.php)
function getRandomCodes(){
$ans = "0123456789";
$sus = strlen($ans) - 1;
return substr($ans, rand(0, $sus), 1) . substr($ans, rand(0, $sus), 1) . substr($ans, rand(0, $sus), 1) . substr($ans, rand(0, $sus), 1) . substr($ans, rand(0, $sus), 1) . substr($ans, rand(0, $sus), 1) . substr($ans, rand(0, $sus), 1). substr($ans, rand(0, $sus), 1). substr($ans, rand(0, $sus), 1). substr($ans, rand(0, $sus), 1). substr($ans, rand(0, $sus), 1). substr($ans, rand(0, $sus), 1);
}
function getRandomCode(){
$an = "01234abcdef5678ghijklm9nopqrxtuvwxyz";
$su = strlen($an) - 1;
return substr($an, rand(0, $su), 1) . substr($an, rand(0, $su), 1) . substr($an, rand(0, $su), 1) . substr($an, rand(0, $su), 1) . substr($an, rand(0, $su), 1) . substr($an, rand(0, $su), 1) . substr($an, rand(0, $su), 1). substr($an, rand(0, $su), 1). substr($an, rand(0, $su), 1). substr($an, rand(0, $su), 1). substr($an, rand(0, $su), 1). substr($an, rand(0, $su), 1);
}
for($i = 0; $i < 20; $i++){
$gen = mysqli_query($mysqli, "insert into pins(serials, pins)values('" .getRandomCode(). "', '" .getRandomCodes(). "')");
}
if($gen){
echo "ok";
}else{
echo $mysqli->error;
}
jQuery
function update_btn_click(param) {
$('#updatebutton').prop("disabled",true);
$.ajax({
url:"generate_cards/card_generate.php",
type:"post",
beforeSend: function()
{
$("#message").fadeOut();
$("#updatebutton").html('Generating <img src="../img/processing.gif" width="30" />');
},
success: function(response){
if(response=='Ok') {
//location.reload();
$("#message").fadeIn(1000, function(){
$("#message").html('<div class="alert alert-success"> <span class="fa fa-check"></span> Successfully Generated </div>');
//$("#btn-submit").html('<i class="fa fa-plus"></i> Add again');
});
cardTable.ajax.reload(null, false);
} else {
$("#message").html('<div class="alert alert-danger"><span class="fa fa-info-circle"></span> '+response+' !</div>');
$('#updatebutton').prop("disabled",false);
}
},
error: function(response){
console.log('could not fetch data');
},
complete: function(response){
// hide loading
}
});
}
有人可以指引我走正确的路吗?
答案 0 :(得分:-1)
我已经可以使用以下方法解决它:
function update_btn_click(param)
{
//$('#updatebutton').prop("disabled",true);
var resp = $("#updatebutton");
$.ajax({
type: "POST", // Method type GET/POST
url: "generate_cards/cards.php", //Ajax Action url
data: {},
// Before call ajax you can do activity like please wait message
beforeSend: function(xhr){
resp.html('Generating <img src="../img/processing.gif" width="30" />');
},
//Will call if method not exists or any error inside php file
error: function(qXHR, textStatus, errorThrow){
resp.html("There are an error");
$("#message").html('<div class="alert alert-danger"><span class="fa fa-info-circle"></span> '+data+' !</div>');
//$('#updatebutton').prop("disabled",false);
},
success: function(data, textStatus, jqXHR){
//resp.html(data);
$("#message").fadeIn(1000, function(){
$("#message").html('<div class="text-success"> <span class="fa fa-check"></span> Successfully Generated </div>');
$("#updatebutton").html('<i class="fa fa-refresh"></i> Generate Cards');
});
cardTable.ajax.reload(null, false);
}
});
}