Solved

Page resubmission

Posted on 2004-09-23
15
367 Views
Last Modified: 2010-08-05
I am fairly new to PHP although I have done quite a bit html/vbscript etc.  I am putting together a page that will take user input and calculate a value from this.  What I want to know is what is the best way of:

a. determining whether the page is being viewed for the first time or whether it has been resubmitted
b. keeping values entered into list boxes after resubmission

Many thanks

Ken
0
Comment
Question by:kenabbott
  • 9
  • 4
  • 2
15 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12131679
>> a

if your form has a submit buton called submit (eg. <input type="submit" name="submit"> )

if (isset($_POST['submit'])) {

 //this code will only be run after the form has been submitted (ie. not on the first view)

}

>> b

example:

 <select name="list">
  <option<?php if (isset($_POST['list']) && $_POST['list'] == "Value1") echo "selected"; ?>>Value1</option>
  <option<?php if (isset($_POST['list']) && $_POST['list'] == "Value2") echo "selected"; ?>>Value2</option>
  <option<?php if (isset($_POST['list']) && $_POST['list'] == "Value3") echo "selected"; ?>>Value3</option>
 </select>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12131692
if the list box is multi select then:

 <select name="list[]" multiple>
  <option<?php if (isset($_POST['list']) && in_array("Value1",$_POST['list'])) echo " selected"; ?>>Value1</option>
  <option<?php if (isset($_POST['list']) && in_array("Value2",$_POST['list'])) echo " selected"; ?>>Value2</option>
  <option<?php if (isset($_POST['list']) && in_array("Value3",$_POST['list'])) echo " selected"; ?>>Value3</option>
 </select>
0
 
LVL 7

Expert Comment

by:jdpipe
ID: 12133220
It will take a bit of work to get your head around it, but take a look at PEAR. It has a great class called QuickForm (under the HTML category) which can help you lay out our forms, and it will display the entered values in the original format of the form.

If you're looking for a bit more DIY, then you need to use approaches like those above. Are your list boxes special - are you talking <select  multiple> or something else?

To ensure that a form isn't being resubmitted, you might want to disable the submit button, or add some javascript functionality liike so:

<script>
var havesubmitted=0;
function onlyOnce(){
      document.forms['f'].elements['sub'].value="wait...";
      document.forms['f'].elements['sub'].disabled=true;
      return true;
}
</script>
<form name="f" action="http://google.de/search" onsubmit="onlyOnce()" method="get">
<input name="q" value="experts exchange"/>
<input name="sub" type="submit" id="submitButton" value="search"/>
</form>
0
 
LVL 7

Expert Comment

by:jdpipe
ID: 12133227
Oops

Pear is at http://pear.php.net/
0
 

Author Comment

by:kenabbott
ID: 12178087
Re the submiting a page how would I code the (isset($_POST['submit']))  part if I wanted the form to resubmit via the onchange event of a list box.  Also how would I handle this if I had a number of list boxes on a page and I wanted to resubmit the page after any of them had been changed?

Thanks

Ken
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12178141
>> Re the submiting a page how would I code the (isset($_POST['submit']))  part if I wanted the form to resubmit via the onchange event of a list box.

Rather then using submit use the name of the select box. The same principles apply other then that, basic eg:

<?php
if (isset($_POST['list'])) {
 //actons do do if the list has "changed"
 echo "form submitted";
}
?>

<form method="post">
 <select name="list" onChange="this.form.submit();">
  <option<?php if (isset($_POST['list']) && $_POST['list'] == "Value1") echo " selected"; ?>>Value1</option>
  <option<?php if (isset($_POST['list']) && $_POST['list'] == "Value2") echo " selected"; ?>>Value2</option>
  <option<?php if (isset($_POST['list']) && $_POST['list'] == "Value3") echo " selected"; ?>>Value3</option>
 </select>
</form>

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12178158
>> Also how would I handle this if I had a number of list boxes on a page and I wanted to resubmit the page after any of them had been changed?

If they are all within the same form then the above method will work fine, if we are talking seperate forms then use sessions.

heres an example with 2 forms:

<?php
session_start(); //at the top of your page

if(isset($_POST['list'])) $_SESSION['list'] = $_POST['list'];
if(isset($_POST['list2'])) $_SESSION['list2'] = $_POST['list2'];
?>

<form method="post">
 <select name="list" onChange="this.form.submit();">
  <option<?php if (isset($_SESSION['list']) && $_SESSION['list'] == "Value1") echo " selected"; ?>>Value1</option>
  <option<?php if (isset($_SESSION['list']) && $_SESSION['list'] == "Value2") echo " selected"; ?>>Value2</option>
  <option<?php if (isset($_SESSION['list']) && $_SESSION['list'] == "Value3") echo " selected"; ?>>Value3</option>
 </select>
</form>

<form method="post">
 <select name="list2" onChange="this.form.submit();">
  <option<?php if (isset($_SESSION['list2']) && $_SESSION['list2'] == "Value4") echo " selected"; ?>>Value4</option>
  <option<?php if (isset($_SESSION['list2']) && $_SESSION['list2'] == "Value5") echo " selected"; ?>>Value5</option>
  <option<?php if (isset($_SESSION['list2']) && $_SESSION['list2'] == "Value6") echo " selected"; ?>>Value6</option>
 </select>
</form>
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:kenabbott
ID: 12178200
The list boxes are all on the same form - so would I include the (isset... code for each list box ie

if (isset($_POST['list1'])) { .......etc

if (isset($_POST['list2'])) { ........etc


0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12178217
The isset part is simply there to prevent you getting notices generated by php (undefined index error) so you don't as such need it in each option if your level of error checking is reduced.

When processing the data sent by the select box you would use it then

if (isset($_POST['list'])) {

 //so that this code is ONLY run when the select box has been submitted

}

However in the select box the minimum you need is, for example:

<form method="post">
 <select name="list" onChange="this.form.submit();">
  <option<?php if ($_SESSION['list'] == "Value1") echo " selected"; ?>>Value1</option>
  <option<?php if ($_SESSION['list'] == "Value2") echo " selected"; ?>>Value2</option>
  <option<?php if ($_SESSION['list'] == "Value3") echo " selected"; ?>>Value3</option>
 </select>
</form>

As i said the isset in the options just prevents an error notice being shown and keeps the code clean, if your php is configured to hide notices you can leave it out if you want.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12178223
sorry in the above _SESSION should be _POST, copied the wrong segment but the theory still applies.
0
 

Author Comment

by:kenabbott
ID: 12178301
Yes my reason for asking the question was that when I originally did the script I was hetting errors when the page first loaded.  It sounds simpler just to hide the notices but...

a) how do you do this
b) is it a good idea i.e. does it create any other potential problems/issues etc?
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12178341
>> a)

one of two main options

1) apply the setting globally in the php.ini file

find

error_reporting  = E_ALL;

and replace with:

error_reporting  = E_ALL & ~E_NOTICE;

2) apply the setting to just this script, probably the better idea in this case if you chose to do this

at the top of your page add

error_reporting(E_ALL ^ E_NOTICE);

>> b)

In most cases no, the script will be fully functional.

The reason for the notice is basically just because you are trying to reference a variable that has not yet been set (prior to the forms submission) so it tells you so. Your script will work fine in this particular case if you hide the notice.

My personal opinion on the other hand is always go the extra mile to keep the code clean and tight, well written code should not produce any error at all however as i said its not a big issue here so its really down to your choice.
0
 

Author Comment

by:kenabbott
ID: 12178498
Yes I agree.  I've done most of my programming in VB and I'd much rather have clean code that aviods all errors.

One further point, I understand the points you're making re the list boxes and variables but is there no way of simply checking if the form/page is on its first load rather than checking if the list boxes have been submitted.  I was trying to find a way of giving a variable a value on first load etc so that this value would identify that the page was reloading rather than loading for the first time.  Is this possible??
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12178548
To do that you would need it globally available so i would look towards sessions again, for example

<?php
session_start();  //top of page

if (!isset($_SESSION['reload'])) {
  $_SESSION['reload'] = 0;
}
else {
  $_SESSION['reload'] = 1;
}
?>

The first time the page is loaded the session variable, reload, will not have been set so you can assign the value 0 to it. Sebsequent reloads of the page will mean that it is set so the value 1 will be set to it.

You could use this method to track how many times the page has been reloaded, eg:

if (!isset($_SESSION['reload'])) {
  $_SESSION['reload'] = 0;
}
else {
  $_SESSION['reload']++;
}

First time around it will equal 0 and each time after that it will be incremented by one.
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 250 total points
ID: 12178568
Note: an alternative way of writing the above is:

$_SESSION['reload'] = (!isset($_SESSION['reload'])) ? 0 : 1;

or for the latter example:

$_SESSION['reload'] = (!isset($_SESSION['reload'])) ? 0 : $_SESSION['reload']+1;
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 33
Wordpress syntax error 1 29
PHP Script - Am I missing anything here? 8 39
Filter wordpress query 3 14
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now