Alert after executing php script while not leaving

2019-09-19 12:39发布

This question already has an answer here:

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

1条回答
Luminary・发光体
2楼-- · 2019-09-19 13:03

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.

查看更多
登录 后发表回答