【问题标题】:Submit -> Execute PHP script -> Alert User -- while staying on same page提交 -> 执行 PHP 脚本 -> 提醒用户——同时停留在同一页面上
【发布时间】:2015-10-12 19:55:05
【问题描述】:

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

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------}

我在Alert after executing php script while not leaving current page 提出了一个类似的问题,该问题在Alert after executing php script while not leaving current page 被标记为重复,但我想出了一个解决方案,所以我想分享一下。

【问题讨论】:

  • -2 没有 cmets 或输入对于 Stack 的新手来说似乎有点苛刻。我确实已经对此进行了研究,我给出了我的代码的清晰示例,但我不知道为什么我会得到如此负面的回应。
  • 只是猜测,但否决票可能是因为您几乎两次发布了相同的问题。我知道另一个被标记为重复,但我不明白为什么你得到了反对票。此外,关于确切问题的信息不足。什么有效,什么无效。你得到什么错误。等等等等
  • 是的,社区对小事可能很苛刻。我有时也结束了。所以有一个 +1 这样你就不会推迟这个惊人的网络

标签: javascript php forms alert submit-button


【解决方案1】:

我可以通过在我的header('location: index.php?text=success) 函数中添加一个 URL 查询字符串来完成此操作,然后使用 JS 我可以使用 if 语句来查找查询字符串并在出现时发出警报。

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

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');

此解决方案的唯一缺点是我无法轻松访问我的 PHP $phone 变量来告诉用户消息发送到哪个号码。

【讨论】:

    【解决方案2】:

    AJAX 是最适合这项工作的方法,因为您要实现的是前端交互。 PHP 是一种服务器端语言。

    AJAX 将表单数据传输到后端 php 脚本。一旦 php 脚本处理了服务器上的数据,它就可以将您需要的数据返回给 AJAX 脚本。这有时使用 JSON 来完成,尤其是当您有多个变量时。

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

    返回的 JSON 代码如下所示:

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

    类似的教程非常有用: [http://www.yourwebskills.com/ajaxintro.php][1]

    我发现,从你的主要项目中休息一下,花一点时间来学习你试图实现的目标背后的机制,可以让你更快地解决你的问题。

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 2011-08-09
      • 2013-06-21
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2018-02-09
      相关资源
      最近更新 更多