javascript - Alert after executing php script while not leaving current page -


this question has answer here:

i have page 2 submit buttons using if ($_post['action'] == 'test sms') executed code "test sms" button. need execute code php script give alert box while not leaving page.

i keep reading ajax way , i've tried, index.php , updateuserconfig.php on different servers, cross-domain errors.

it seems i'm not using header() function correctly current code want. executes script without leaving page, i'm not getting alert popup.

i've tried redirect js, won't redirect until "ok" clicked on alert button, until browser directed empty page don't want.

i've tried using js on index.html, alert before script executed, , don't have easy access php variable alert phone number test text 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 test sms button     //grab ntid , phone header    if(isset($_post['ntid'])) $ntid = $_post['ntid'];    if(isset($_post['phone'])) $phone = $_post['phone'];     //using notify_sms_users funtion send_notification.php    require 'send_notification.php';    notify_sms_users(array($ntid), "", 4);     //alert user there message has been sent    $alert = "your message has been sent " . $phone;    echo '<script type="text/javascript">alert("'.$alert.'");';    echo '</script>';     header('location: index.php');  } else {-----action other submit button------} 

this not easy... if submit form iframe, that's considered out of fashion today. however, minimal changes can work current code - have change form tag , add iframe element:

<iframe name="myiframe" id="myiframe"></iframe> <form action="updateuserconfig.php" method="post" target="myiframe"> 

this way can alert after post, since receiving php run inside iframe, you'll have use javascript navigate parent (the original) page. part easy - unusual. you'll have a

parent.location="mynextpage.php" 

to make happen. also, has limitations cross-domain thing too. (for these issues, google "access-control-allow-origin" official solution - , you'll see dirty workarounds too.)

with ajax, of course, different. can't explain in single post - needs reversed approach, submit form, php script handled post and then original javascript make alert. wouldn't recommend way if haven't done before.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -