2015-05-12 68 views
0

這裏是我的表單代碼PHP重定向到不同的頁面表格後提交

<form action="PHP/form.php" id="msform" method="post"> 

<fieldset id="owner_service"> 
<h2> ARE YOU A DOG OWNER OR SERVICE PROVIDER?</h2> 
<legend>owner_service</legend> 
<div class="owner_service"> 
<input type="radio" id="service" name="owner_service" value="service"> 
<label for ="service"><h5>SERVICE PROVIDER</h5></label> 
<input type="radio" id="owner" name="owner_service" value="owner"> 
<label for ="owner"><h5>DOG OWNER</h5></label> 
</div> 
<input type="button" name="next" class="next action-button" id="next" value="NEXT" /> 
</fieldset> 

第一位和這裏的PHP

<?php 
session_start(); 

$servername = ""; 
$username = ""; 
$password = ""; 
$dbname = ""; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 


$sql = "INSERT INTO pets (owner_service, Gender, Age, Size, Location, idealLocation, Service) 
VALUES ('{$_POST['owner_service']}', '{$_POST['gender']}', '{$_POST['age']}', '{$_POST['size']}', '$locationCommaString', '{$_POST['ideal_location']}', '{$_POST['service']}')"; 



if($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 


?> 

我有我的形式運行,所以結果是數據庫,但是我想要做的是根據用戶點擊「服務提供商」還是「狗主人」,將用戶帶到新的頁面。我不知道在哪裏放置標題,因爲如果我替換已有的if語句,那麼結果將不會顯示在我的數據庫中。

回答

0
if(isset($_POST['next'])) { 

    // your code to save data 
    // after submit without db error 

    if($_POST['owner_service'] == 'service') { // redirect page; } 
    else if($_POST['owner_service'] == 'owner') { // redirect page; } 

} 

把它放在同一個回聲的範圍「新記錄創建成功」;

0

if語句的查詢執行後進入。什麼@laimingl建議

0
<?php 
session_start(); 

$servername = ""; 
$username = ""; 
$password = ""; 
$dbname = ""; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 


$sql = "INSERT INTO pets (owner_service, Gender, Age, Size, Location, idealLocation, Service) 
VALUES ('{$_POST['owner_service']}', '{$_POST['gender']}', '{$_POST['age']}', '{$_POST['size']}', '$locationCommaString', '{$_POST['ideal_location']}', '{$_POST['service']}')"; 



if($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
    $serv = $_POST['owner_service'] ; 
    switch($serv){ 
    case 'case 1': 
      // Page redirection code here 
    break; 

    case 'case 2': 
    // Page redirection code here 
    break; 





} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 


?> 
+0

嘿此感謝,但是當我嘗試這個代碼,它只是帶我到「回聲;頁面,如果我嘗試刪除該代碼,它會將我帶到一個空白頁面。我會告訴你我的代碼 if($ conn-> query($ sql)=== TRUE){ $ serv = $ _POST ['owner_service']; 開關($ SERV){ 殼體 '殼體1': 頭( '位置:service_form.html'); \t \t exit; 休息; 情況下 '殼體2': \t \t頭( '位置:owner_form.html'); \t \t exit; 休息; } } else { echo「Error:」。 $ sql。 「
」。 $ conn->的錯誤; } – laimingl

+0

您的問題是否已解決? –

+0

我設法使用javascript document.location來解決它 – laimingl

-1

類似只需更改input type="button"input type="submit"