Jump to content

If [form validated] submit Form1 ELSE submit Form2?


jonnewbie12

Recommended Posts

Hi.  Pretty straight forward I guess but as the name suggests am a newbie.  I have a form that requires the user to enter certain parameters.  If the values are blank it submits to itself and loads the error messages.  What I want to do is create PHP code that submits the form to a different url.  What I thought was create two forms (the second with hidden fields replicating the first form), each form having a different url in the action"" code.  What I cant work out is the PHP IF ELSE code to submit form 2 if Form1 is is validated correctly.  This is the PHP code relevant to the form validation.  Help?

 

<?php

//If form was submitted

if ($_POST['submitted']==1) {

    $errormsg = ""; //Initialize errors

    if ($_POST[width]){

    $title = $_POST[width]; //If title was entered

    }

    else{

    $errormsg = "Please enter width";

    }

    if ($_POST[drop]){

    $textentry = $_POST[drop]; //If comment was entered

    }

    else{

    if ($errormsg){ //If there is already an error, add next error

    $errormsg = $errormsg . " & content";

    }else{

    $errormsg = "Please enter drop";

    }

    }

}

 

if ($errormsg){ //If any errors display them

    echo "<div class=\"box red\">$errormsg</div>";

}

 

//If all fields present

if ($title && $textentry){

  //Do something

   

echo 'THIS IS WHERE I WANT THE CODE TO SUBMIT FORM 2 or SUBMIT FORM 1 TO A DIFFERENT URL';

}

?>

Link to comment
Share on other sites

What do you mean you want it to submit to a different url? If you're describing what I think you are, your best bet is to simply use a conditional for the form's action= attribute, based on whether the form validated or not, and present it to the user in the same form as an "Are You Sure?" type of thing with the fields set to read-only or disabled, and another submit button.

 

So:

1. The user completes and submits the form.

2. Validation routine runs

3. If validation succeeds, the same form is presented, with a different action= attribute, and all fields set to disabled to prevent further edits

4 User submits form again, this time it goes to a different url.

 

If that isn't what you're talking about, provide more details.

Link to comment
Share on other sites

Thank you Pickachu thats exactly what I mean.  If the form validation is successful I want to the page to submit the form to a new URL.  If the validation is unsuccessful I want it to submit to itself and display the error message.

 

Can you include PHP in the "action" function of the form so it says only submit if validation is successful?  HOw would you do that for example?

 

p.s. I will try the 'header' idea now too

Link to comment
Share on other sites

Thanks Gotharius. Don't know anything about Ajax so will try and work with PHP if all else fails will go there.

 

Pickahu:  I have tried the following script in the form action button so that if the width is 1 it submits to one url and if not it submits to another: Is that what you meant?  It still doesn't seem to work when i is entered though and always submits to the 'template5.php'

 

<form id="form1" name="form1" method="post" action=<?php if ($_POST['width']==1) echo "template3formvalidation.php"; else echo "template5.php";?>>

Link to comment
Share on other sites

I think whats happening is that the PHP code is returning an empty value in the width box as the form has not submitted itself prior to the PHP query.  On the second attempt it works as the form has now submitted the value so it directs to the right page.  Is it possible then to:

 

1. Enter the width value

2. Submit the form to itself

3. Then run the PHP code in the action button query the value of the width field

4. If the with value is =1 resubmit and it will go to template5.php.

5. If the width value is not 1 it will resubmit to itself again.

 

Problem is I want all this done in one click??  AHGGG

Link to comment
Share on other sites

Header doesnt work.  Error code says I cannot modify header information

 

Headers have to be set before any output to the browser. You have set your header after some output has been sent to the browser, which is why you are getting the error.

Link to comment
Share on other sites

Sorry totally confused now:

 

Have looked at AJAX and seems like a huge learning curve and only works on most recent browsers.

 

Not sure what you mean by setting the header beforehand?

 

Best chance of keeping this within the realsm of my limited knowlede is to try and keep to PHP.

 

I thought this was quite a simple process but maybe I am not making myself clear enough.

 

All I want to do is the following:

 

1. User inputs some info into a form field (for my purposes 'width')

2. User presses submit button on form

3. If width equals a certain value or is empty a message is displayed on same page saying value is wrong or empty

4. If width value is ok (i.e. validated), form is submitted to a different page where the value can then be displayed.

 

The form validation by itself seems easy enough providing you don' want to return two different outcomes depending on the submitted form.

 

Happy for anyone to take this from a fresh perspective and start from scratch but at the moment I am totally confused?  Sorry to take your time up with this.

 

 

Link to comment
Share on other sites

AJAX isn't your best method anyways, as the user may disable javascript, and the form would not be validated before submission.

 

As for the header comment. Look at this:

 

<html>
<?php header('location:form2.php'); ?>

 

With this code, the HTML tag is sent to the browser when that line of code happens. Then the location() function is called after - this will fail. However, if you do it this way:

 

header('location:form2.php');
<html>

 

The header is before the HTML tag, so this is ok (assuming you have no other output before this code). In this case, the redirect will happen.

 

Side note: AJAX works in pretty much all browsers. Even IE6

Link to comment
Share on other sites

Doesnt matter where I put the code:

 

<?php header('location:http://www.mysite.co.uk'); ?>

 

I just keep getting this message

 

Warning: Cannot modify header information - headers already sent by (output started at /websites/my host/LinuxPackage22/mysite.co.uk/public_html/form2.php:73) in /websites/myhost/LinuxPackage22/mysite.co.uk/public_html/form2.php on line 73

 

:(

 

Link to comment
Share on other sites

this is the code as copied from the example in my test page:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>

</head>

 

<body>

 

<?php

 

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

$err = array();

if( empty($_POST['name']) )

$err['name'] = 'Name is a required field';

elseif( !preg_match('/^[a-z]++$/i', $_POST['name']) )

$err['name'] = 'Name can only be letters';

 

if( empty($_POST['email']) )

$err['email']  = 'Email is a required field';

elseif( !preg_match('/@/', $_POST['email']) )

$err['email'] = 'Email must contain @ symbol';

 

if( empty($err) ) {

//function_to_process_data();

//header( 'Location: http://yoursite.com/success.php' );

echo header ('Location: http://www.mysite/template5.php' );

exit();

}

}

 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>form</title>

</head>

<body>

<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">

<label>Name: <input type="text" name="name" value="<?php if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>"></label>

<?php if(isset($err['name'])) echo '<span style="color: red;">'.$err['name'].'</span>' ?>

<br>

<label>Email: <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>"></label>

<?php if(isset($err['email'])) echo '<span style="color: red;">'.$err['email'].'</span>' ?>

<br>

<input type="submit"><input type="reset">

</form>

</body>

</html>

 

 

</body>

</html>

Link to comment
Share on other sites

You need to re-read the few posts above yours and realize what you're doing.

 

If you can't figure out why you're getting those messages, or whats inherently wrong with the above posted markup/code, then you really need to go back to the basics fo HTML markup and leave PHP alone until you understand that concept.

Link to comment
Share on other sites

You are outputting a doctype, then a bunch of HTML, then your PHP code. Your PHP code has the location() call in it. As I've mentioned a couple of times (and even gave the line number), you have to move the location call before any HTML output to the browser.

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.