Jump to content
Sign in to follow this  
Chrex

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

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.

Share this post


Link to post
Share on other sites

Which example are you referring to? A form will automatically submit all of the data to the action, unless Javascript interferes somehow.

Share this post


Link to post
Share on other sites

I refer: w3schools.com/php/php_form_complete.asp

 

I dont use a Javascript. If I understood the exampe right, it submits the variables back to the form.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

a general rule for forms is:

 

validate input

if(input is valid)

{

execute some code, eg. include page2.php to show the confirmation form}

else{

execute some code, eg. include page1.php to show the first form}

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Hey guys,

 

I´m using a session now. For now it is an easy solution. I´ll see if it will work with my future requirements. Thanks for your help again!

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...