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