简体   繁体   English

提交->执行PHP脚本->警报用户-停留在同一页面上

[英]Submit -> Execute PHP script -> Alert User — while staying on same page

I have a page with two submit buttons using if ($_POST['action'] == 'Test SMS') to executed code for my "Test SMS" button. 我有一个带有两个提交按钮的页面,这些按钮使用if ($_POST['action'] == 'Test SMS')执行我的“ Test SMS”按钮的代码。 I need to execute code from a PHP script then give an alert box while not leaving the page. 我需要从PHP脚本执行代码,然后在不离开页面的情况下提供一个警告框。

index.html 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 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------}

I asked a similar question that was marked a duplicate at Alert after executing php script while not leaving current page but was able to come up with a solution so I wanted to share. 我问了一个类似的问题,该问题在执行php脚本后没有离开当前页面的情况下Alert上被标记为重复,但能够提出解决方案,所以我想分享。

I was able to accomplish this by adding a URL query string in my header('location: index.php?text=success) function then using JS I was able to use an if statement to look for the query string and alert if so. 通过在header('location: index.php?text=success)函数中添加URL查询字符串,然后使用JS,我能够使用if语句查找查询字符串并发出警报,以实现此目的。

index.html 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>

<script type="text/javascript">
$(document).ready(function () {
    if(window.location.href.indexOf("settings=success") > -1) {
       alert("Your settings have been saved");
    }
    else if(window.location.href.indexOf("text=success") > -1) {
       alert("A SMS has been sent!");
    }
});
</script>

updateUserConfig.php 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);

   header('Location: index.php?text=success');

} else {-----action for other submit button------}
    header('Location: index.php?settings=success');

The only downside to this solution is that I don't have easy access to my PHP $phone variable to tell the user what number the message was sent to. 该解决方案的唯一缺点是,我无法轻松访问PHP $phone变量来告诉用户消息发送到的号码。

AJAX is the method most suited to this job, because what you are trying to achieve is a frontend interaction. AJAX是最适合此工作的方法,因为您要实现的是前端交互。 Php is a server side language. php是服务器端语言。

AJAX will transmit form data to the backend php script. AJAX将表单数据传输到后端php脚本。 Once the the php script has handled the data on the server, it can return the data you require to the AJAX script. php脚本处理完服务器上的数据后,便可以将所需的数据返回给AJAX脚本。 This is done sometimes using JSON, especially as you have multiple variables. 有时使用JSON可以完成此操作,尤其是当您有多个变量时。

$formdata = array(
    'ntid' => $_POST['ntid'],
    'phone' => $_POST['phone']
);

return json_encode($formdata);

The returned JSON code will look something like: 返回的JSON代码如下所示:

{"ntid":"NT ID","phone":"Phone number"}

Tutorials similar to this are very useful: [ http://www.yourwebskills.com/ajaxintro.php][1] 与此类似的教程非常有用:[ http://www.yourwebskills.com/ajaxintro.php] [1 ]

I have found that taking a break from your main project and investing a little time in learning the mechanics behind what your trying to achieve, enables you to solve your problem faster. 我发现,从您的主要项目中休息一下,花一点时间来学习尝试实现的背后的机制,可以使您更快地解决问题。

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

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