Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate a form / Display results on same page

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,516
Scotland
Hello All.

This is probably very simple, but I can't figure it out. In summary, this is what I want to achieve:

1. Display a form from which the user can upload a file.

2. Perform some checks on the uploaded file.

3. Display the results of those checks on the same page as the original form.

This is what I've got so far (highly simplified for the sake of clarity):

Code:
<form enctype="multipart/form-data" action = "check.php" 
  method="post">
  <input type = "hidden" name = "MAX_FILE_SIZE" 
    value = "50000" />
  <p>File to upload:</p>
  <input type = "file" name = "importfile" size = "50" 
    accept = "text/plain" />
  <input type = "submit" value = "Upload Now" />
</form>

The form's action attribute points to a page named check.php, which consists simply of a call to a function named upload(). The function looks like this (again, highly simplified):

Code:
if ( .... the validation checks fail ...)
  { echo 'File is invalid; exit; }
else
  { echo 'Success'; }

This all works fine, apart from one detail: The error / success message appears on a new page. Not surprising, given that check.php is a separate page from the original form.

What I want is for the message to appear on the same page as the original form. In other words, I don't want the user to have to navigate back to the previous page in order to repeat the upload.

Obviously, this must be possible, as I've seen this type of behaviour a million times, but I can't see how to do it. Any help greatly appreciated.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
It's supposed to work by putting on action="<?php echo $_SERVER['PHP_SELF']; ?>" and by putting the content of HTML file on the same file as the php file. Anyway I haven't managed to make it work correctly, as I get a range of weird errors. If you manage to make it work properly, please post it here. Hope this helps.
 
Very simplistically...

Make sure you put a name attribute on your submit button.

Submit the form to itself then at the top of the script do a check for the presence of the submit in the post array.

If the submit is there, do the validation and other processing.

If the submit isn't there, then show the form.

Code:
if($_POST['submit']){
 ... do processing stuff ...
} else {
 ... show form ...
}

Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Kyru,

Thanks for the reply. I hadn't realised you could directly use a script as an action attribute in that way.

I'll keep it in mind, but for now I'm going to try Foamcow's suggestion.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Foamcow,

Your suggestion works perfectly. Many thanks.

Just one point ...

In order to test whether the submit button is present in the post array, I am doing this (where "submit_button" is the name I gave the button):

Code:
foreach ($_POST as $key => $value)
{
  if ($key = "submit_button" )
  {
  // do the validation here
  break;
  }
}

Is this the best way to search an array for the presence of a given key, or is there a shorter way of doing it? (In Visual FoxPro, I would use ASCAN()).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Your code is going to loop through all the keys and not do anything unless it finds the submit key.

To me, this seems laborious. There is no point creating the foreach loop.

If you want to take that approach then you could simply use array_key_exists(), like so:

Code:
if(array_key_exists('submit_button',$_POST){
   ...validation...
}


So if the key exists it will return 'true' and execute the code within. If the key isn't there then the result is 'false' and the code is skipped.

Generally I do it the dirty way with

Code:
if($_POST['submit_button']){

}

Which is probably wrong in a whole heap of ways but it works.



Probably a dirty way to do it, but it will work.




Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Foamcow,

Your code is going to loop through all the keys and not do anything unless it finds the submit key.

To me, this seems laborious. There is no point creating the foreach loop.

Which is precisely why I asked the question.

I didn't know about array_key_exists(). Needless to say, it works perfectly. Thanks to both you and Feherke for pointing it out.

By the way, I originally tried to do if($_POST['submit_button']), but this generated a "variable not found" error. I wonder why mine is different than yours.

Anyway, the problem's solved now. Thanks again.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
By the way, I originally tried to do if($_POST['submit_button']), but this generated a "variable not found" error. I wonder why mine is different than yours.

Because my php ini settings restrict the level of error messages I see. The error is being generated but it's not fatal.

Like I said...
Which is probably wrong in a whole heap of ways but it works.
:)

For live environments you are best turning the errors off anyway.

Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Foamcow,

For live environments you are best turning the errors off anyway.

I plan to read up on the whole error-handling scene before I go into production running -- which is still quite a long way off.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Just for completion sake:

I always use the isset() function to check whether a variable exists or more accurately is set.

In the case of form submitted variables its very good as it will not generate an error if they don't exist, it just returns false so it can be simply used as:

Code:
if(isset($_POST['submit_name'])){
process form.
}
else{
form was not submitted, so show form.
}





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for that, Vacunita. I remember now that I read about isset() when I was perusing the manual, but it wasn't in my brain when I needed it. I'll keep it in mind for the future.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top