matleeds Posted November 22, 2011 Share Posted November 22, 2011 I'm fairly new to html/javascript/php and a bit confused. I've inherited a partially complete web app that seems to make great use of include()..as in including php files. here's the flow index page loads. index page includes(login.php) user logs in ok index page includes(admin.php) - the admin page, with a link to 'settings' user clicks on settings index page includes (settings.php) - this extracts the settings from a db and displays them on the page within the <form> tag user amends settings and submits form - settings are updated to db ok now, here's the thing after the submit process it looks like the settngs page is reloaded/refreshed? but the original settings are displayed. If i click on the browser 'page refresh' it does refresh with the updated/new settings values. i'd like 2 things, firstly a message box to appear after the submit saying 'settings updated successfully' and the settings to display/retain the updated details. (without having to click on the browser refresh. the solution maybe vey simple (hopefully) but like i say, i'm fairly new to the html/javascript/php side of things if there is a refresh page function i can use..which file name do i use..index.php or settings.php or is there a better way of doing it? ..i don't understand why it doesn't retain the updated details after the form submit? Here's the code for index.php .... <?php $dir = dirname(__FILE__); require_once "$dir/ot/ot.php"; ot::include_view('header', array('account' => null)) ?> <html> <head> <title>Welcome to ....</title> </head> <body style="font-size: 14pt; font-family=verdana;"> <div><img src="OTLogo1.bmp"/><h1> Welcome to ...</h1> </div> <?php if (!empty($account)): ?> <div style="border-bottom: 1px dotted #AAA; padding-bottom: 2px; margin-bottom: 10px;"> <div style="float: left"> <?php $mtime = (int)@file_get_contents(otDB_DIR."/updated"); $date = date("d/m/Y", $mtime); $time = date("G:i", $mtime); if ($mtime > 0) { echo "Last Updated $date at $time"; } ?> </div> <div style="float: right">Welcome, <?php echo $account->email;?> - <a href="?page=home">Home</a> - <?php ot::include_view('logout_link')?></div> <div style="clear: both"></div> </div> <?php if (ot::is_admin()) { ot::include_view('admin_page'); } else { ot::include_view('user_page'); } ?> <?php else: ?> <p>Please login below.</p> <?php ot::include_view('login_form')?> <?php endif; ?> </body> </html> and appointment-settings.php... <?php $settings = ot::db('settings')->fetch_all(); ?> <?php if ($settings): ?> <?php foreach($settings as $s): ?> <?php $smsEnabled = $s->smsEnabled; $emailEnabled = $s->emailEnabled; $smsMessage = $s->smsMessage; $emailMessage = $s->emailMessage; $daysBefore = $s->daysBefore; $spendLimit = $s->smsSpendLimit; ?> <script language="javascript" type="text/javascript"> <!-- function imposeMaxLength(Object, MaxLen) { return (Object.value.length <= MaxLen); } function validateForm() { var x=document.forms["settings"]["smsMessage"].value; var x=document.forms["settings"]["emailMessage"].value; if (x==null || x=="" || y==null || y=="") { alert("Both SMS and Email messages must be filled out."); return false; } } --> </script> <form name="settings" action="<?php echo $_SERVER['REQUEST_URI']?>" method="POST" onsubmit="return validateForm();"> <p>SMS Enabled: <select name="smsEnabled" size="1"> <option value="true" <?php echo($smsEnabled == 1 ? 'selected="selected"' : '');?>>Yes</option> <option value="false" <?php echo($smsEnabled == 0 ? 'selected="selected"' : '');?>>No</option> </select> <p>Email Enabled: <select name="emailEnabled" size="1"> <option value="true" <?php echo($emailEnabled == 1 ? 'selected="selected"' : '');?>>Yes</option> <option value="false" <?php echo($emailEnabled == 0 ? 'selected="selected"' : '');?>>No</option> </select> <p><b>Note:</b> The maximun character length for the SMS message is 160. Please ensure that both the SMS and Email messages include the {patient name} and {date} tags.</b></p> <p>SMS Message: <textarea rows="2" cols="80" name="smsMessage" onkeypress="return imposeMaxLength(this, 160);" ><?php echo $smsMessage?></textarea></p> <p>Email Message: <textarea rows="2" cols="80" name="emailMessage" onkeypress="return imposeMaxLength(this, 160);" ><?php echo $emailMessage?></textarea></p> <p>Days before appointment to send the reminder: <select name="daysBefore" size="1"> <option value="1" <?php echo($daysBefore==1 ? 'selected="selected"' : '');?>>1</option> <option value="2" <?php echo($daysBefore==2 ? 'selected="selected"' : '');?>>2</option> <option value="3" <?php echo($daysBefore==3 ? 'selected="selected"' : '');?>>3</option> <option value="4" <?php echo($daysBefore==4 ? 'selected="selected"' : '');?>>4</option> <option value="5" <?php echo($daysBefore==5 ? 'selected="selected"' : '');?>>5</option> <option value="6" <?php echo($daysBefore==6 ? 'selected="selected"' : '');?>>6</option> <option value="7" <?php echo($daysBefore==7 ? 'selected="selected"' : '');?>>7</option> </select> <p>Spend Limit per month (£): <input type="text" name="smsSpendLimit" size="6" maxlength="5" value="<?php echo($spendLimit);?>" readonly="yes"/></p> <p>Total spend / left this month (£): <input type="text" name="smsSpendLeft" size="6" maxlength="5" value="<?php echo($spendLimit);?>" readonly="yes"/><p> <p><input type="submit" value="Save settings" /><p> </form> <?php endforeach ?> <?php else: ?> <p>Currently unable to access the Settings table. Please contact your customer support</p> <?php endif ?> <?php if(isset($_POST["smsEnabled"])) { $db = ot::db('settings'); if ($db) { $smsEnb = ($_POST["smsEnabled"] == 'true' ? 1 : 0); $emailEnb = ($_POST["emailEnabled"] == 'true' ? 1 : 0); $db->update('smsEnabled=?,emailEnabled=?,smsMessage=?, emailMessage=?, daysBefore=?, smsSpendLimit=?', 'id = ?', array($smsEnb, $emailEnb, $_POST["smsMessage"], $_POST["emailMessage"], $_POST["daysBefore"], $_POST["smsSpendLimit"],1)); } echo "<p>Settings updated</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251609-form-update-issue/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 22, 2011 Share Posted November 22, 2011 The reason for the need to refresh the page to get the 'updated' settings to be displayed, is because the logic on the page is out of order. The code gets the settings and produces the form first, then it updates any changes. Since programming does not involve magic or the ability to go back in time, the current code cannot display the updated information unless it is rewritten to process the form submission first, then get and display the settings second. Quote Link to comment https://forums.phpfreaks.com/topic/251609-form-update-issue/#findComment-1290384 Share on other sites More sharing options...
matleeds Posted November 22, 2011 Author Share Posted November 22, 2011 thanks bud. i assumed when the page kind of reloaded after a submit that it would grab the db settings again...but alas not...you live and burn Quote Link to comment https://forums.phpfreaks.com/topic/251609-form-update-issue/#findComment-1290403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.