0

I got a problem storing my form values. My goal is to submit my first form, save the values inside a variable, redirect to my second form and submit/store all form values inside my database(SQL).

HTML

<form action="process.php" class="form" method=POST name="firstform" onsubmit="return chkForm()">
      <label id="form_title">Bevor wir anfangen können, benötigen wir noch ein paar Informationen von Ihnen:</label>
      <br/>
      <br/>
      <label for="name">Benutzername</label> 
      <input type="text" id="name" name="name" value="">

      <label for="email">Email</label>  
      <input type="email" id="email" name="email" value="">

      <button type="submit" name="submit" value="abschicken">Eingaben absenden</button>
    </form>

process.php

try {
$connect = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connect->beginTransaction();   
if (!empty($_POST['firstform'])) {
    $alias = $_POST['name'];
    $mail = $_POST['email'];
    echo '<script type="text/javascript">window.document.location.href = "http://www.example.de/form2.php";</script>';
    exit;
}
if(isset($_POST['fullform'])){ 
  $connect->exec("INSERT INTO user (name, email, answer_0, answer_1, answer_2, answer_3, answer_4, answer_5, answer_6, answer_7, answer_8, answer_9, answer_10, answer_11, answer_12, answer_13, answer_14, answer_15, answer_16, answer_17) VALUES ('$alias', '$mail', '$_POST[answer_0]', '$_POST[answer_1]', '$_POST[answer_2]', '$_POST[answer_3]', '$_POST[answer_4]', '$_POST[answer_5]', '$_POST[answer_6]', '$_POST[answer_7]', '$_POST[answer_8]', '$_POST[answer_9]', '$_POST[answer_10]', '$_POST[answer_11]', '$_POST[answer_12]', '$_POST[answer_13]', '$_POST[answer_14]', '$_POST[answer_15]', '$_POST[answer_16]', '$_POST[answer_17]');");

} 
$connect->commit();
echo "Danke Für Ihre Teilnahme";
echo $_POST['email'];
exit;

Why is my script not redirecting after i saved the values into $alias and $mail.

12
  • change to window.location.href Commented Apr 8, 2016 at 10:06
  • try using var_dump($_POST['firstform']); and check if if condition works according to your requirements? Do you get javascript on your page? Commented Apr 8, 2016 at 10:08
  • hmm...var_dump gives me NULL, but i dont understand why edit: if i change the if statement to if (!empty($_POST['mail'])) i get my redirect (but no value gets saved) How can firstform be null, but mail not? Commented Apr 8, 2016 at 10:19
  • Because you're using the form's name, which doesn't give anything. Only inputs with names will be in the POST-array. Commented Apr 8, 2016 at 10:30
  • It can be null cause you are not sending a variable named 'firstform' in your request. Modify your first form and add this variable Commented Apr 8, 2016 at 10:30

2 Answers 2

1

You can use header("location:http://www.example.de/form2.php"); instead of javascript.

If you want use javascript then use window.location instead of window.document.location.href

echo "<script type="text/javascript">window.location = 'http://www.example.de/form2.php';</script>";
Sign up to request clarification or add additional context in comments.

1 Comment

But JavaScript is better if you need to show some text before redirect.
0

On a fast search on the site, i found that and could help you.

// similar behavior as an HTTP redirect:

window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link:

window.location.href = "http://stackoverflow.com";

How to redirect to another webpage in JavaScript/jQuery?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.