2015-12-14 42 views
0

我有一個動態頁面(holidays.php),在URL holidays.php使用get方法是這樣的ID來的數據?ID = 1如何與形式發送ID在PHP

代碼

include('header.php'); 
include('db.php'); 
$id = $_GET['id']; 

$sql_hp = "select * from `holiday_package` where `id` ='$id'"; 
$res_hp = mysql_query($sql_hp); 
$rec_hp = mysql_fetch_array($res_hp); 

和我有一個聯繫表格在同一頁面,客戶端填寫併發送電子郵件給管理員。

<form action="holidays-form.php" method="post"> 
      <!--<span class="success"><?php echo $smsg; ?></span> 
      <span class="fail"><?php echo $emsg; ?></span>--> 

      <h3>Book Package</h3> 
      <div class="form-group"> 
      <div class="form-elements"> 
      <label>Name</label> 
      <div class="form-item"> 
       <input type="text" name="name" required="required"> 
       </div> 
      </div> 
      </div> 

      <div class="form-group"> 
      <div class="form-elements"> 
      <label>Email ID</label> 
      <div class="form-item"> 
       <input type="email" name="email" required="required"> 
       </div> 
      </div> 
      </div> 

      <div class="form-group"> 
      <div class="form-elements"> 
      <label>Contact Number</label> 
      <div class="form-item"> 
       <input type="text" name="mobile" required="required"> 
       </div> 
      </div> 
      </div> 

      <div class="form-select-date"> 
      <div class="form-elements"> 
       <label>Select Travel Date</label> 
       <div class="form-item"><i class="awe-icon awe-icon-calendar"></i> 
       <input type="text" name="date" class="awe-calendar" value="Date" required="required"> 
       </div> 
      </div> 
      </div> 
      <div class="form-select-date hide"> 
      <div class="form-elements"> 
       <label>Select Package</label> 
       <div class="form-item"> 
       <input type="hidden" name="pkg_title" value="<?php echo $rec_hp['pkg_title'];?>"> 

       </div> 
      </div> 
      </div> 

      <div class="form-group"> 
      <div class="form-elements hide"> 
      <label>Package Price</label> 
      <div class="form-item"> 
       <input type="hidden" name="pkg_price" value="<?php echo $rec_hp['pkg_price'];?>" /> 
       </div> 
      </div> 
      </div> 


      <div class="form-group" style="margin-top:-15px;"> 
      <div class="form-elements"> 
       <label>No. of Guest</label> 
       <div class="form-item"> 
       <select name="adult" required> 
        <option value="1">1</option> 
        <option value="2">2</option> 
        <option value="3">3</option> 
        <option value="4">4</option> 
        <option value="5">5</option> 
        <option value="6">6</option> 
       </select> 
       </div> 
      </div> 
      </div> 

      <div class="price"><em>Total Cost for this Package</em> <div class="amount" id="cost_dv">INR <?php echo $rec_hp['pkg_price'];?>/-</div></div> 
      <div class="form-submit"> 
      <div class="add-to-cart"> 
       <button type="submit">Send Query for Booking</button> 
      </div> 
      </div> 



      </form> 

我經過假期-form.php的頁面發送郵件,請參閱下面

<?php 

    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $mobile = $_POST['mobile']; 
    $date = $_POST['date']; 
    $pkg_title = $_POST['pkg_title']; 
    $pkg_price = $_POST['pkg_price']; 
    $adult = $_POST['adult']; 
    $from = 'Company Name'; 

    $to = "[email protected]"; 
    $subject = "New Holiday Package Submission"; 

    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 



    $message = '<html><body> 

<table width="500" border="0" cellspacing="0" cellpadding="0" style="border:#ccc solid 1px;"> 

     <tr> 
       <td width="151" height="30" style="background:#f3f3f3;  padding-left:5px;">Name</td> 
       <td width="149" style="background:#f3f3f3; padding- right:5px;">'.$name.'</td> 
    </tr> 
     <tr> 
      <td height="30" style="padding-left:5px;">Email</td> 
      <td style="padding-right:5px;">'.$email.'</td> 
     </tr> 

     <tr> 
      <td height="30" style="background:#f3f3f3; padding-left:5px;">Mobile</td> 
      <td style="background:#f3f3f3; padding-right:5px;">'.$mobile.'</td> 
      </tr> 
     <tr> 
      <td height="30" style="padding-left:5px;">Date</td> 
      <td style="padding-right:5px;">'.$date.'</td> 
     </tr> 

     <tr> 
      <td height="30" style="background:#f3f3f3; padding-left:5px;">Package Name</td> 
      <td style="background:#f3f3f3; padding-right:5px;">'.$pkg_title.'</td> 
      </tr> 

      <tr> 
      <td height="30" style="padding-left:5px;">Package Price (adult) </td> 
      <td style="padding-right:5px;">'.$pkg_price.'</td> 
     </tr> 

     <tr> 
      <td height="30" style="background:#f3f3f3; padding-left:5px;">Package Price (child)</td> 
      <td style="background:#f3f3f3; padding-right:5px;">'.$adult.'</td> 
      </tr> 



    </table> 
    </body></html>'; 

$success = mail($to,$subject,$message,$headers); 

if ($success == true) { 

header("Location:holidays.php?s=mail sent"); 
} 
    else { 
    header("Location:holidays.php?s=error"); 
} 


?> 

我想表明在holidays.php成功消息的代碼,我已經寫了像表單標籤代碼此

<?php 

    $s = $_GET['s']; 
    if ($s=="mail sent") { 
     echo ('<span class="success">Thankyou! Your Package Has been  Successfully Submitted!. We will Feedback you asap.</span>'); 
    } 

    else if ($s=="error") { 
    echo ('<span class="fail">Sorry! Please ensure you have filled all fields Correctly.</span>'); 
    } 

但是提交示出這樣的錯誤形式holidays.php頁後(注:未定義指數:ID在C:\ XAMPP \ htdocs中\棘手-旅行者\第4行holidays.php)成功messag即所以我如何存儲或發送我在頁面頂部使用的$ id值。

+1

請不要使用'mysql_ *'函數,因爲它們已被棄用,並且在最近發佈的PHP 7.0中使用[mysqli](http://php.net/manual/en/book.mysqli.php)或[ PDO](http://php.net/manual/en/book.pdo.php)。 – BRoebie

回答

2

給隱藏<input />場(如果你使用GET法):

<input type="hidden" name="id" value="<?php echo $id; ?>" /> 

如果使用POST方法,只需更改你的PHP爲:

$id = $_POST['id']; 

或更改<form>標籤到:

<form action="holidays-form.php?id=<?php echo $id; ?>" method="post"> 
+0

檢查對不起哈哈 –

+0

它不工作先生,請告訴一些簡單的方法 –

+0

不要使用'先生'。你嘗試了什麼,還有什麼比這更簡單的方法? –

0

你可以使用一個隱藏的表單字段

<input type="hidden" name="pkg_price" value="<?php echo htmlentities($_REQUEST['id']);?>" /> 

,並確保您檢查$id時更改$_GET$_REQUEST,因爲您發佈的形式(所以它會出現在$_POST$_GET)。

+0

我試過get方法,但仍然無法正常工作 –