2011-12-08 62 views
4

我有2個程序welcome.php和form.jsp。我打算通過用戶形式使用odbc將值插入到SQL數據庫中。我已經使用form.jsp創建了一個用戶窗體。使用welcome.php插入行。而插入不插入重複的值,以便檢查while循環中的條件。當if語句正在工作時,由於exit()語句,它完全從程序中出來。在執行完退出後得到空白頁面。但我需要保留回到用戶窗體或控件應該去form.jsp,以便我應該得到用戶窗體輸入的細節沒有給空白page.how我可以這樣做嗎?控制應該去if語句中的其他程序使用php

 $query1="select * from company"; 
     $result1 = odbc_exec($connect, $query1); 

     while(odbc_fetch_row($result1)){ 
     $compname[$index] = odbc_result($result1, 1); 
     $empname[$index] = odbc_result($result1, 2); 

     if($compname[$index]==$_POST['cname']) 
     { 
     echo "<script> alert(\"compname Exists\") </script>"; 
     exit(); 
     //exit("<script> alert(\"compname Exists\") </script>"); 
     } 
     if($empname[$index]==$_POST['ename']) 
     { 
     echo "<script> alert(\"empname Exists\") </script>"; 
     exit(); 
    } 
    } 
    $query=("INSERT INTO dbo.urllink(cname,ename) VALUES ('$_POST[cname]','$_POST[ename]') "); 
    $result = odbc_exec($connect, $query); 
    echo "<script> alert(\"Row Inserted\") </script>"; 

?> 

回答

1

使用exit()停止程序的處理。將用戶從PHP另一個頁面,則可以使用header()功能,用「地點:」參數:

header('Location: form.jsp'); 

請注意,你不能輸出信息(使用echo例如),然後使用header功能。所有輸出都需要通過發送給他們的頁面來完成。

如果你想傳遞的信息,如提交的字段存在,你可以使用查詢字符串參數將其發送,例如:然後

header('Location: form.jsp?error=abc'); 

你的JSP頁面將取決於什麼abc意味着顯示錯誤。

或者,您可以使用會話將更復雜的信息傳遞到另一個頁面。 你可以找到更多的信息會議here

+1

請注意,您還可以利用會話數據避免需要通過URL傳回大量數據。 –

+0

@威爾比克福德,好點。 – Cyntech

+0

我應該爲abc寫函數嗎? – user998461

0

您也可以使用JavaScript。

window.location = "your location path where you want to redirect"; 

        OR 

window.location.href = "your location path where you want to redirect"; 

我認爲這也可能對您有所幫助。

謝謝。