Jump to content

Recommended Posts

I have a form which I know displays correctly but when I call it as a function to my php file it doesn't display.  Do I need to tell the parser to pause & wait for the user to fill out the form prior to executing the rest of the script?  If so how do I do that?

OR

the error is apparently on the validation line which I thought was caused because the $POST array didn't get a chance to be created by the form submission.  If the parser goes through every line of code to validate it before executing it could just be a syntax error in the validation line.

 

The error I get is: Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\Guana_Plant_Key\test.php on line 14

 

Here is my code

<?php
include ("functions.php");
$to_show = array();  					// make checkbox storage array - list of pics to show
$to_show = $_POST["to_show"];
$group_by = $_POST["group_by"];
browsing_form ();

if (array_key_exists('_submit_check' $_POST)) and if (array_key_exists('1' $_POST)) {

//execute a bunch of code here

} else {
browsing_form ();
echo 'Please use the submit button to submit this form and make sure one type of picture is selected.'; 
?>

 

One other question.  Assuming I get this working and the user messes up the first time they submit the form so the else part of the function gets activated will the program then go back and execute my "bunch of code" or do I need to put it in again after the else statement?

 

Link to comment
https://forums.phpfreaks.com/topic/110321-newb-form-validation-question/
Share on other sites

Thanks violent.

 

There is something else wrong with my validation statement probably in the "and" part though I can bypass the second half just by making it into a comment which I did so I could see if the form would appear.

 

apparently I have something else wrong with my code because when it gets to the end of it all it gives me this error

 

Parse error: syntax error, unexpected $end in C:\wamp\www\Guana_Plant_Key\test.php on line 95.  It was running through fine before I started to include the forms and validate them.

 

What does that error mean??

Duh ::)

 

Thanks Barand.

 

Ok now we are back to my original question.  The form is now displaying but it doesn't wait for user input.  Instead it just runs through the whole script so the form isn't validated and it redisplays it with the error message I created in the "else" part of of the validation statement.  So the end result is you see the form twice and then get the error message.  Is there a way to make the parser pause to allow the user to fill in the form before it redisplays it with the error message?

 

Thanks,

 

Kate

You're going to have to change your way of thinking about program flow.  It's not like real time environments like a stand alone app where it "waits" for an "event."  It takes the code, parses it, spits back the results, and it's all over.  It's like shaking a magic 8 ball and asking questions over and over. The 8 ball doesn't really "wait" for you to ask a question.  You have to setup conditions like..a tumbling block, or a filter. 

OK . . .

What I understand is

If I write my form as a function & then call it into the main php file then the php will just run right through it without allowing the user to input anything.  I like the idea of using a function because it makes the main script much cleaner and its easier to see whats going on.  I guess I can code it all in html though if neccesary.

 

I thought I read that this could be done as a function though.  Am I just confused???

Sorry to be so dense this is my first attempt at a php script as well as using a form to get user info.

 

Kate

You can write it as a function, yes, but all of your script will be parsed, and all output sent to the browser, after it's parsed. so...

 

<?php

  function showform () {
echo <<<SOMEFORM
<form action = 'target.php' method = 'post'>
      <input type = 'text' name = 'blah1'><br />
      <input type = 'text' name = 'blah2'><br />
      <input type = 'submit' value = 'submit'>
   </form>
SOMEFORM;
} // end form

 

...is a valid function.  But you can't call it and wait for the user to get back to you before continuing your script.  So you just have to setup conditions to check for it, and execute script, depending on it.  This isn't the "best" way of doing it, but it's simplified for example purposes:

 

<?php

function showform () {
echo <<<SOMEFORM
<form action = 'target.php' method = 'post'>
      <input type = 'text' name = 'blah1'><br />
      <input type = 'text' name = 'blah2'></br />
      <input type = 'submit' value = 'submit'>
   </form>
SOMEFORM;
} // end form

// no posted vars? show form
if (!$_POST) {
   showform();
// else...something was posted, so...
} else {
   // if nothing was posted in blah1...
   if(!$_POST['blah1']) {
      // make an error msg
      $errormsg = "fill out blah1!<br />";
   } // end if
   // if nothing was posted in blah2...
   if(!$_POST['blah2']) {
      // make an error msg (or rather, add to it, cuz one might already have been started
      $errormsg.= "fill out blah12<br />";
   } // end if
   
   // something not filled out? echo msg and show form
   if($errormsg) {
      echo "$errormsg<br />";
      showform();
   // everything seems to be in order so...
   } else {
      // do something with the posted vars
   } // end else
} // end else
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.