简体   繁体   English

php执行时,jQuery“请等待”

[英]jQuery “please wait” while php executes

I have a script that connects to the PayPal api to check for a valid credit card input. 我有一个连接到PayPal api的脚本来检查有效的信用卡输入。 The script can take about 5 seconds to execute and in an effort to keep users from clicking "submit" multiple times if they don't see an immediate response, I'd like to place a "Please wait" indicator. 该脚本可能需要大约5秒钟才能执行,并且为了防止用户多次点击“提交”,如果他们没有看到立即响应,我想放置一个“请稍候”指示符。

I have a div, "pleaseWait" which is hidden. 我有一个div,“pleaseWait”是隐藏的。 In jQuery I have: 在jQuery我有:

$('#submit').click(function(){
    $('#pleaseWait').show();
});

The only problem is if there is an issue, it will send the php error back and the "Please wait" will continue on screen. 唯一的问题是如果有问题,它将发回php错误,“请等待”将继续在屏幕上。 I decided to try another approach and echo the jQuery in php after the script starts to run, and hide it if there is an error. 我决定尝试另一种方法,并在脚本开始运行后在php中回显jQuery,并在出现错误时隐藏它。

 /* Echo the jQuery so the "Please wait" indicator will show on screen */
 echo "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js\"></script>";
 echo "<script type='text/javascript' charset='utf-8'>";
 echo "\$(document).ready(function(){";
 echo "\$('#pleaseWait').show();";
 echo "});";
 echo "</script>";


 if($error == ""){

      /* There is not an error, run php. */

 }else{
      /* There was an error, stop the jQuery from displaying the "please wait" and display the error */
      echo "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js\"></script>";
      echo "<script type='text/javascript' charset='utf-8'>";
      echo "\$(document).ready(function(){";
      echo "\$('#pleaseWait').hide();";
      echo "});";
      echo "</script>";
 }

This can work, but seems really messy. 这可以工作,但似乎非常混乱。 Is there a better way to do this other than multiple echos in php? 除了PHP中的多个回声之外,还有更好的方法吗?

try use ajax 尝试使用ajax

$('#submit').click(function(){
    $('#pleaseWait').show();
    $.post('url',card,function(data){
         $('#pleaseWait').hide();
         alert(data);
    })
});

Use $.ajax() , and have the PHP script reply with JSON that can be read in separate success/error callbacks, or a single complete callback. 使用$.ajax() ,让PHP脚本回复JSON,可以在单独的success/error回调或单个complete回调中读取。

There's a number of jQuery plugins that give you easy ajaxified forms, ready made. 有许多jQuery插件可以为您提供简单的ajaxified表单,现成的。 Mike Alsup's jQuery Form Plugin is an especially popular one. Mike Alsup的jQuery Form插件是一个特别受欢迎的插件


Alternately, skip the ajax call entirely, and just disable the submit button when the form is submitted: 或者,完全跳过ajax调用,并在提交表单时禁用提交按钮:

$('#submit').click(function(){
    this.disabled = true;
});

I can't say that I fully understand your question but I'll try to answer what I can. 我不能说我完全理解你的问题,但我会尽力回答我的问题。 If multiple echos are your problem, try jumping in and out of php: 如果您的问题有多个回声,请尝试跳入和跳出php:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type='text/javascript' charset='utf-8'>
$(document).ready(function(){
$('#pleaseWait').show();
});
</script>

<?php
if($error == ""){
      /* There is not an error, run php. */
 }else{
?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type='text/javascript' charset='utf-8'>
$(document).ready(function(){
$('#pleaseWait').hide();
});
</script>

<?php } ?>

But perhaps your problem is that the php is running first, before the page loads, and javascript is running after (or as) the page loads. 但也许你的问题是php在页面加载之前首先运行,并且javascript在页面加载之后运行(或作为)。

It's not exactly what you were originally asking for, but from experience, the best way to prevent a user from clicking submit multiple times during a long processing (like an AJAX call) is to just disable the submit button . 它并不完全是您最初要求的,但根据经验,阻止用户在长时间处理(如AJAX调用)中多次单击提交的最佳方法是禁用提交按钮

Don't get me wrong, I still think that you should put in a "please wait" message or something. 不要误会我的意思,我仍然认为你应该加入“请稍候”的消息或其他什么。 What I'm saying is that let your message concentrate on just being a message, and let your button concentrate on not letting itself get clicked. 我所说的是让你的信息集中在一个消息上,让你的按钮专注于不让自己被点击。

It's simple, it doesn't have to keep a lot of complexity in check, and it works. 它很简单,它不需要在检查中保持很多复杂性,并且它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM