在不离开当前页面的情况下执行php脚本后警告 [英] Alert after executing php script while not leaving current page

查看:128
本文介绍了在不离开当前页面的情况下执行php脚本后警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用两个提交按钮的页面if($ _POST ['action'] =='Test SMS')来执行我的测试短信按钮。我需要从一个PHP脚本执行代码,然后在不离开页面的情况下给出一个警告框。



我一直在阅读ajax是做到这一点的方式,而且我已经尝试过,但index.php和updateUserConfig.php在不同的服务器上,所以我得到了跨域错误。



似乎我没有使用 header()函数正确,但当前代码几乎完全符合我的要求。它在不离开页面的情况下执行脚本,但我没有收到警报弹出窗口。



我试着用js重定向,但不会重定向到点击我的提醒按钮,确定,直到那时浏览器被引导到我不想要的空白页面。

我试过在index.html上使用JS,但是在脚本执行前我得到了警报,并且我没有简单的访问PHP变量提醒测试文本被发送到的电话号码。

index.html

 < form action =updateUserConfig。 phpmethod =post> 
< input type ='submit'name ='action'value ='Test SMS'class ='btn-test'>
< input type =submitname =actionvalue =Saveclass =btn btn-primary>
< / form>

updateUserConfig.php

  if($ _POST ['action'] =='Test SMS'){//测试短信按钮的动作

//从头部抓取ntid和手机
if(isset($ _ POST ['ntid']))$ ntid = $ _POST ['ntid'];
if(isset($ _ POST ['phone']))$ phone = $ _POST ['phone'];

//使用send_notification.php中的notify_sms_users函数
require'send_notification.php';
notify_sms_users(array($ ntid),,4);

//提醒用户有消息已发送
$ alert =您的消息已发送到。 $电话;
echo'< script type =text / javascript> alert('。$ alert。');';
echo'< / script>';

header('Location:index.php');

} else {-----其他提交按钮的动作------}


解决方案

这并不容易......如果您将表单提交到IFRAME中,您可以这样做,但现在已经过时了。但是,只需稍作更改就可以使用当前代码 - 只需更改表单标签并添加iframe元素即可:

 < iframe name =myIframeid =myIframe>< / iframe> 
< form action =updateUserConfig.phpmethod =posttarget =myIframe>

通过这种方式,您可以在POST后发出警报,但由于接收的php会在< iframe ,则必须使用javascript来浏览(原始)页面。这部分很简单 - 非常不寻常。你必须做一个

  parent.location =mynextpage.php

来实现这一目标。而且,这对于跨域的东西也有限制。 (对于这些问题,谷歌的官方解决方案访问控制 - 允许 - 起源 - 你可能会看到一些肮脏的解决方法。)



AJAX,当然,一切都不一样。但是我无法在一篇文章中解释 - 它需要一种相反的方法,您提交表单,从处理该文章的PHP脚本中取回某些内容,然后您的原始JavaScript将使警报。如果您之前没有这样做,我不会推荐这种方式。


I have a page with two submit buttons using if ($_POST['action'] == 'Test SMS') to executed code for my "Test SMS" button. I need to execute code from a PHP script then give an alert box while not leaving the page.

I keep reading that ajax is the way to do this and I've tried, but index.php and updateUserConfig.php are on different servers, so I get cross-domain errors.

It also seems I'm not using the header() function correctly but the current code almost does exactly what I want. It executes the script without leaving the page, but I'm not getting the alert popup.

I've tried to redirect with js, but it won't redirect until "OK" is clicked on my alert button, so until then the browser is directed to an empty page that I don't want.

I've tried using JS on index.html, but I get the alert before the script is executed, and I don't have easy access to the PHP variable to alert what phone number a test text was sent to.

index.html

<form action="updateUserConfig.php" method="post">
<input type='submit' name='action' value='Test SMS' class='btn-test'>
<input type="submit" name="action" value="Save" class="btn btn-primary">
</form>

updateUserConfig.php

if ($_POST['action'] == 'Test SMS') { //action for Test SMS Button

   //grab ntid and phone from header
   if(isset($_POST['ntid'])) $ntid = $_POST['ntid'];
   if(isset($_POST['phone'])) $phone = $_POST['phone'];

   //using the notify_sms_users funtion from send_notification.php
   require 'send_notification.php';
   notify_sms_users(array($ntid), "", 4);

   //alert user that there message has been sent
   $alert = "Your message has been sent to " . $phone;
   echo '<script type="text/javascript">alert("'.$alert.'");';
   echo '</script>';

   header('Location: index.php');

} else {-----action for other submit button------}

解决方案

This is not easy... You could do it like this if you submit your form into an IFRAME, but that's considered out of fashion today. However, with minimal changes it can work with your current code - you only have to change the form tag and add an iframe element:

<iframe name="myIframe" id="myIframe"></iframe>
<form action="updateUserConfig.php" method="post" target="myIframe">

This way you can alert after the POST, but since the receiving php will run inside the iframe, you'll have to use javascript to navigate the parent (the original) page. That part is easy - just very unusual. You'll have to do a

parent.location="mynextpage.php"

to make that happen. Also, this has limitations for the cross-domain thing too. (For these issues, google for "Access-Control-Allow-Origin" which is the official solution - and you'll probably see some dirty workarounds too.)

With AJAX, of course, everything is different. But I can't explain that in a single post - it needs a reversed approach, you submit the form, get something back from the php script that handled the post and then your original javascript will make the alert. I wouldn't recommend this way if you haven't done it before.

这篇关于在不离开当前页面的情况下执行php脚本后警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆