2013-03-25 30 views
0

我正在製作一個jQuery移動web應用程序,我正在使用多頁面佈局。我在頁面加載時連接到mySQL,並在第二個div頁面上,我有一個表單,使用php文件在提交時將表單中的值發送到mySQL數據庫。每當我刷新jquery移動頁面時行被插入數據庫

我的問題是,每次刷新Web應用程序時,都會將一行插入到數據庫的表中。我明顯不希望這種情況發生,直到我點擊/點擊提交按鈕。我對Web應用程序開發比較陌生,希望得到任何幫助,看看我做錯了什麼。

關於如何在多頁面佈局上通過php最好地插入mySQL數據庫的建議會很好。請讓我知道是否需要展示更多代碼。

<!--Top of second page within multi page layout containing form--> 
<!-- The POST A JOURNEY page where user creates a journey to be seen by others--> 
    <div data-role="page" id="postAJourney" data-add-back-btn="true" data-back-btn-text="back" data-rel="back" data-direction="reverse" data-transition="flip" data-theme="b" > 
     <div data-role="header" data-position="fixed"> 
      <h1>Post A Journey</h1> 
     </div> 

     <div data-role="content"> 
      <form action="add_journey.php" method="post" id="postAJourneyForm"> 

<!--Top of multipage layout--> 
<?php 
require 'database.php'; 
?> 

再次更新

<?php 

//get the journey data 
$radUserType = intval($_POST['radUserType']); 
$pjFrom = $_POST['pjFrom']; 
$pjTo = $_POST['pjTo']; 
$radioJourneyType = intval($_POST['radioJourneyType']); 
$departDate = $_POST['departDate']; 
$returnDate = $_POST['returnDate']; 
$textareanotes = $_POST['textAreaNotes']; 

//check all values from $_POST 
$canSubmitForm = false; 
$isFormEmpty = false; 

if (empty($radUserType) || empty($pjFrom) || empty($pjTo) || empty($radioJourneyType) || empty($departDate) || empty($returnDate) || empty($textareanotes)) 
{ 
$isFormEmpty = true; 
} 

//check for your data here 
if(isset($radUserType) && isset($pjFrom) && isset($pjTo) && isset($radioJourneyType) &&  isset($departDate) && isset($returnDate) && isset($textareanotes)) 
{ 
$canSubmitForm = true; 
} 

$departTime = '11:12'; 
$returnTime = '11:16'; 
$seatcounter = '2'; 

//will only get executed if true 
if ($canSubmitForm) 
{ 
if ($isFormEmpty == false) 
{ 
require_once('database.php'); 
$query = "INSERT INTO journey 
    (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type) 
     VALUES('$pjFrom','$pjTo','$radioJourneyType','$departDate','$departTime','$returnDate','$returnTime','$seatcounter','$textareanotes','$radUserType')"; 
$db->exec($query); 
// Perform Query 
//$result = mysql_query($query); 
//if($result === true) 
//{ 
// $result = 'success'; 
//} 
//else 
//{ 
// $result = 'insertion failure' . mysql_error(); 
//} 
include('index.php'); 
} 
} 
else 
{ 
$error = "Invalid product data. Check all fields and try again."; 
include('error.php'); 
} 
//Display the Product List page 
?> 
+0

是的,我們展示了他是如何形成的被處理。 – 2013-03-25 12:37:59

+1

第一類:只使用HTTP-GET方法搜索/獲取數據。使用POST進行插入/更新/刪除。 – Teson 2013-03-25 12:39:54

回答

1

你需要打出來的插入邏輯和檢查的形式是有效的,並準備提交。

僞代碼:

// check all values from $_POST 
$canSubmitForm = false; 

// check for your data here 
// might want to verify type as well 
if (isset($radUserType) && isset($pjFrom) && isset($pjTo) && isset($radioJourneyType)) 
{ 
    $canSubmitForm = true; 
} 

// will only get executed if true 
if ($canSubmitForm) 
{ 
    require_once('database.php'); 
    $query = "INSERT INTO journey 
       (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_ti  me,seats_available,journey_message,user_type) 
       VALUES('$pjFrom','$pjTo','$radioJourneyType','$departDate','$departTime','$returnDate','$re turnTime','$seatcounter','$textareanotes','$radUserType')"; 

    $db->exec($query); 
    // Perform Query 
    $result = mysql_query($query); 

    // the rest of your code here 
    ... 
} 
+0

感謝您的幫助Phill。我現在在提交之前檢查表單是否有效。但是,如果我將表單元素留空,它仍會對數據庫執行插入操作。然後它重定向到空白的url http://localhost/share/add_journey.php。我會在 – 2013-03-25 14:07:20

+1

以上顯示我更新的php。您可能還需要檢查空值,空白等等,或檢查值以及 – 2013-03-25 16:03:05

+0

雅你的權利。謝謝。我更新了add_journey.php以檢查是否有空值。我仍然感到困惑,爲什麼我提交後我重定向到localhost/share/add_journey.php,這是空白的。當我刷新此頁面時,我看到了我的錯誤消息。 – 2013-03-25 17:24:14

相關問題