如何在php

时间:2015-12-14 11:21:16

标签: php jquery mysql

我有一个动态页面(holidays.php),来自id的数据使用get方法,如url holidays.php?id = 1

代码是

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>

我通过holidays-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 = "info@companyname.com";
  $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页面后显示错误信息(注意:第4行的C:\ xampp \ htdocs \ tricky-traveler \ holidays.php中的未定义索引:id),并显示成功消息。那么如何存储或发送我在页面顶部使用的$ id值。

3 个答案:

答案 0 :(得分: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">

答案 1 :(得分:0)

您可以使用隐藏的表单字段

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

请确保在检查$_GET时将$_REQUEST更改为$id,因为您发布了表单(因此它会显示在$_POST或{{1 }})。

答案 2 :(得分:0)

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

//and in php page get your id using  

$id = $_GET['id'];

$sql_hp = "select * from `holiday_package` where `id` ='$id'";