我遇到了存儲我的表單值的問題。我的目標是提交我的第一個表單,保存變量內的值,重定向到我的第二個表單,並在我的數據庫(SQL)中提交/存儲所有表單值。PHP重定向不會被執行?
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;
爲什麼沒有進行重定向我的劇本後,我保存的值到別名$,$郵件。
變化window.location.href –
嘗試使用'的var_dump($ _ POST [ 'firstform']);'並檢查是否'if'條件時,根據您的要求的作品?你的網頁上有JavaScript嗎? –
hmm ... var_dump給了我NULL,但我不明白爲什麼編輯:如果我改變if語句if(!empty($ _ POST ['mail']))我得到我的重定向(但沒有值得到保存)如何可以先形成空,但郵件不? – Cornykid