Jump to content

Help with a 3 step form


JRhodes88

Recommended Posts

Hello Everybody! This is my first post on this forum and im excited to be a member.

 

I have a class project going on currently and wanted to get some help with it. The project is to create a 3 step form with empty field validation. The guidelines are it has to be all on one php page, no global variables, no SESSION variables.

 

So far this is what I have...

I have 3 functions:

 

1. form_start() - displays the form with all the inputs and checks from for empty fields when the "Preview Answers" button is selected.

 

2. form_preview() - displays the form information that was submitted from and also gives two submit buttons (Edit & Finish).

 

3. form_finish() - displays "Thank you, your data has been submitted!".

 

After I define all three of these functions I have an IF Statement to check to see if any field is left blank. If a field is blank, a flag variable ($something_missing) is set to 1. If nothing is missing and the "Preview Answers" button is selected the flag variable is set to 0.

 

The next IF Statement sees if $something_missing is equal to 0 and what submit button is being selected. These correspond to calling each different function.

 

The problem I am currently having is the form starts, checks for empty fields (and shows appropriate errors), but it will not proceed to the next function. I think it is getting stuck somewhere in the IF Statement, but I cannot figure it out for the life of me.

 

I'm pretty sure the problem is from the code not keeping the value of a variable. I also have a div that contains the values of some different variables.

 

Can anyone see what is going on?

A link to the PHPS file is here: http://sulley.dm.ucf.edu/~jrhodes/dig3134/assignment03-part1/form_all.phps

 

Thanks!

-Justin

Link to comment
Share on other sites

I think you are over complicating this, here is another example on how you could run a post data check, should give you a pointer on how to do it a little easier - it is far from complete but thats not the point.

<?php

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

$req = array("name", "email");

foreach($_POST as $key => $value){
	if(in_array($key, $req) && empty($value)){
		$err_empty[] = $key." is blank";
	}
	${$key} = $value; // Note that this is raw value not suited for direct db insert
}
if(!empty($err_empty)){
  $err_msg = "Following failed:";
  $err_msg .= "<ul><li>";
  $err_msg .= implode("</li><li>", $err_empty);
  $err_msg .= "</li></ul>";
  echo $err_msg;
}
else{
	// treat data based on preview or save
}
}

// show form and use posted values

?>

Link to comment
Share on other sites

if(!$something_missing)
        {
            form_start();
        }
        else if((something_missing==0) && ($dowhat=="Preview Answers"))
        {
            form_preview();
        }
        else if((something_missing==0) && ($dowhat=="Edit"))
        {
            form_start();
        }
        else if((something_missing==0) && ($dowhat=="Finish"))
        {
            form_finish();
        }

 

the something_missing s have something_missing (ha ha). they are missing a $ on the left hand side of them. there's a start.

Link to comment
Share on other sites

Im sorry if I was unclear in my original post. I'm not having problems with the field validation or cookies...those work fine. I understand that my code is probably not the easiest way of doing it, but thats the way the professor taught us for now.

 

The problem I am having is calling each function in the correct manner.

 

- The function form_start() should be called when the page is loaded

  - When the user selects preview it should call the form_preview() function

  - form_preview should only be activated if no field is empty (i was using a flag variable to to this)

- in form_preview the user has two options

  - submit the form and run form_finish()

  - edit the form, which would return them to the beginning form and allow for editing.

Link to comment
Share on other sites

i am pretty sure you have a ! next to the first $something_missing in the first if() statement that you don't want. if something is missing, you want the form_start() to load don't you?

 

if the page is not advancing in the $dowhat actions, you have either a logical problem in your if() conditions, or a data validation problem in the data used in the if() blocks.

 

is the form_start() output being loaded? from what i can understand, your page is doing this:

 

  declare functions

  check if data is missing (on first run of course this is true)

  start if () checks

      if something IS NOT missing

        form_start()

      else if something IS NOT missing AND $dowhat == blah

        form_whateverToSuit$doWhat

 

on your first run, something will always be missing. so how can any of your if() conditions be met?

Link to comment
Share on other sites

Yes, I thought that didnt make much sense so I took out the "!" so it reads IF something is missing, run form_start

 

but now it just displays an empty div, it's not running the form_start function for some reason. By the way, I am updating the PHP file as we go along, so the PHPS file will be updated with each error I try to correct

Link to comment
Share on other sites

i have  no idea why your function calls are not outputting the code that you want.

 

trace the flow of the program like this:

if($something_missing==1)
        {
            print('We are in form_start()<br/>'."\n");
            form_start();
        }
        else if(($something_missing==0) && ($dowhat=="Preview Answers"))
        {
            print('We are in form_preview()<br/>'."\n");
            form_preview();
        }
        else if(($something_missing==0) && ($dowhat=="Edit"))
        {
            print('We are in form_start() - Edit<br/>'."\n");
            form_start();
        }
        else if(($something_missing==0) && ($dowhat=="Finish"))
        {
            print('We are in form_finish()<br/>'."\n");
            form_finish();
        } 

If there is a problem with the function calls, it will at least output a debug message to watch where the flow of execution is going. If you don't get any debug messages, then flow is not entering those code blocks which means there is another problem preventing that and needs to be fixed.

 

Also check any php error log files you have on your host. It may be putting warnings or errors in there that may help.

Link to comment
Share on other sites

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.