Jump to content

Submit PhP-Form after proving | 2nd button? script?


Chrex

Recommended Posts

Hi there, I´m new at this forum and a beginner with PHP. I used the here offert "PHP - Complete Form Example" and it works fine. Thanks for that.But the submit button just proves the form and doesnt send the variables to the next page. Does anyone know a way to send the proven variables to another page? I tried: 1) Print the variable on my secound page with echo $var; --> that didnt work. I guess my secound page doesnt know where to get the variable from. I also dont know where and if my variables are saved after I press the submit button on my page.

 

2) Create two actions in the form like <form action="<?php echo htmlspecialchars($SERVER[...]);?>"> action=page2.php method="post">

That also didnt work. Probably because you cant use 2 actions in one form.

 

3) I dont want to work with url links, because you can see the variables in the url.

 

4) I dont want to work with sessions.

 

5) One of my ideas was to work with another php-script after my form, which will send my variables from the form to the secound page. I figured out how to activate the scrip, when the form is filled out correctly, but I have no idea how to send my data.

 

 

How would it work with the here offert form?

Thanks for your help guys.

Link to comment
Share on other sites

Are you trying to send data from a html form to php? or from a html form to php then to another php page?

 

if the latter, you can store the data into a database in the first php page, then in the second php page you can retrieve the data from the database.

sessions might be more applicable though, despite you not wanting to use them.

 

you can only use 1 action attribute in a html <form> tag.

  • Like 1
Link to comment
Share on other sites

<?php// define variables and set to empty values$nameErr = $emailErr = $genderErr = $websiteErr = "";$name = $email = $gender = $comment = $website = "";$Error = "0";if ($_SERVER["REQUEST_METHOD"] == "POST") {if (empty($_POST["name"])) {$nameErr = "Name is required";$Error = "1";} else {$name = test_input($_POST["name"]);// check if name only contains letters and whitespaceif (!preg_match("/^[a-zA-Z ]*$/",$name)) {$nameErr = "Only letters and white space allowed";}}if (empty($_POST["email"])) {$emailErr = "Email is required";$Error = "1";} else {$email = test_input($_POST["email"]);// check if e-mail address syntax is validif (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) {$emailErr = "Invalid email format";}}if (empty($_POST["website"])) {$website = "";} else {$website = test_input($_POST["website"]);// check if URL address syntax is valid (this regular expression also allows dashes in the URL)if (!preg_match("/b(??:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)) {$websiteErr = "Invalid URL";$Error = "1";}}if (empty($_POST["comment"])) {$comment = "";} else {$comment = test_input($_POST["comment"]);}if (empty($_POST["gender"])) {$genderErr = "Gender is required";$Error = "1";} else {$gender = test_input($_POST["gender"]);}}function test_input($data) {$data = trim($data);$data = stripslashes($data);$data = htmlspecialchars($data);return $data;}?><h2>Headline</h2><p><span class="error">* required field.</span></p><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">Name: <input type="text" name="name" value="<?php echo $name;?>"/><span class="error">* <?php echo $nameErr;?></span><br/><br/>E-mail: <input type="text" name="email" value="<?php echo $email;?>"/><span class="error">* <?php echo $emailErr;?></span><br/><br/>Website: <input type="text" name="website" value="<?php echo $website;?>"/><span class="error"><?php echo $websiteErr;?></span><br/><br/>Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br/><br/>Gender:<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female"/>Female<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male"/>Male<span class="error">* <?php echo $genderErr;?></span><br/><br/><input type="submit" name="submit" value="Submit"/></form><form method="post" action="page2.php"><input type="submit" name="submit" value="Weiter"/></form><?phpif ($Error == 0 && $name != "") {echo "<script>window.location = 'http://......com/page2.php'</script>";}?>

Thats my code on page1.php.

 

On the bottum I tried to insert a 2nd form. (doesnt work)

You can find my idea number 5) below that. (doesnt work)

 

Actually I´m not sure if this is a html-form, because it contains php.

How ever, I try to send the variables to page2.php.

 

Is there an easy way to do it? I dont know if a database is the right tool, because I dont rellay want to store the data.

My goal is a summary on page2.php and to give the user a chance to prove his entered values. After that the data will be put in a XML and send to an email. That means I dont need the data after this point any longer.

Link to comment
Share on other sites

You can only submit one form at a time. The form on top contains several fields and will submit all of them to the same page. The form on bottom does not contain any fields, only a submit button, so if you press that submit button then it will submit to page2.php and the only item in $_POST will be "submit" with the value "Weiter". It doesn't submit any of the other fields because the fields are not in that form.

  • Like 1
Link to comment
Share on other sites

Forget everything below the first form.....that doesnt work.

 

The code until the end of the first form is from "here", from w3schools (see my reference). How would they transfer the data to page2.php?

Link to comment
Share on other sites

If(input is valid)  {   <form method="post"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">   <input type="submit" name="submit" value="Submit"/>   </form>   }else   {   <form method="post"  action="page2.php">   <input type="submit" name="submit" value="Submit"/>   </form>   }

Thanks, that could solve the problem. I just dont know where to put the form.

Would it be like the code above? Wouldnt I have two forms then?

Link to comment
Share on other sites

In order to send data to another page you can put the variables in the URL and access them in $_GET, build a form that uses $_POST and add the data as hidden inputs, or store the data in the session and access it on the following page. I'm confused about why you need a second page though, you can do everything in one file.

Would it be like the code above? Wouldnt I have two forms then?

The if/else will only show one form at a time.
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...