提交 -> 执行 PHP 脚本 -> 提醒用户 - 同时停留在同一页面
Submit -> Execute PHP script -> Alert User -- while staying on same page
我有一个包含两个提交按钮的页面,使用 if ($_POST['action'] == 'Test SMS')
为我的 "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 被标记为重复,但能够想出一个解决方案,所以我想分享。
我能够通过在我的 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
变量来告诉用户消息发送到的号码。
AJAX是最适合这个工作的方法,因为你要实现的是前端交互。 Php 是一种服务器端语言。
AJAX 将表单数据传输到后端 php 脚本。一旦 php 脚本处理了服务器上的数据,它就可以 return 向 AJAX 脚本提供您需要的数据。有时使用 JSON 完成此操作,尤其是当您有多个变量时。
$formdata = array(
'ntid' => $_POST['ntid'],
'phone' => $_POST['phone']
);
return json_encode($formdata);
returned JSON 代码将类似于:
{"ntid":"NT ID","phone":"Phone number"}
类似这样的教程很有用:
[http://www.yourwebskills.com/ajaxintro.php][1]
我发现从您的主要项目中抽身而出,花点时间学习您想要实现的目标背后的机制,可以让您更快地解决问题。
我有一个包含两个提交按钮的页面,使用 if ($_POST['action'] == 'Test SMS')
为我的 "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 被标记为重复,但能够想出一个解决方案,所以我想分享。
我能够通过在我的 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
变量来告诉用户消息发送到的号码。
AJAX是最适合这个工作的方法,因为你要实现的是前端交互。 Php 是一种服务器端语言。
AJAX 将表单数据传输到后端 php 脚本。一旦 php 脚本处理了服务器上的数据,它就可以 return 向 AJAX 脚本提供您需要的数据。有时使用 JSON 完成此操作,尤其是当您有多个变量时。
$formdata = array(
'ntid' => $_POST['ntid'],
'phone' => $_POST['phone']
);
return json_encode($formdata);
returned JSON 代码将类似于:
{"ntid":"NT ID","phone":"Phone number"}
类似这样的教程很有用: [http://www.yourwebskills.com/ajaxintro.php][1]
我发现从您的主要项目中抽身而出,花点时间学习您想要实现的目标背后的机制,可以让您更快地解决问题。