2014-04-08 95 views
-3

我想用JavaScript來驗證我的聯繫方式,但我不知道驗證碼,在哪裏包括驗證使用javascript在PHP

我想驗證重定向到thank_you.php頁

之前應該發生PLZ幫我...

HTML

<fieldset> 
    <?php include("formcode.php"); ?> 
</fieldset> 

formcode.php

<form method='post' action="database.php"> 
    NAME: <input type='text' name='name' id='name' /> 
    Email: <input type='text' name='email' id='email' /> 
    Contact: <input type='text' name='contact' id='contact' /> 
    Tour: 
    <select id="tour" name="tour" > 
     <option value="">--Select Tours--</option> 
     <option value="Tour1">Tour1</option> 
     <option value="Tour2">Tour2</option> 

    </select><br/> 
    Location: 
    <select id="location" name="location" > 
     <option value="">--Select Your Location--</option> 
     <option value="Loc1">Loc1</option> 
     <option value="Loc2">Loc2</option> 
    </select><br/> 
    Comment:<br /> 
    <textarea name='comment' id='comment' /></textarea><br /> 

    <input type='submit' value='Submit' /> 

</form> 

database.php中

<?php 
if($_POST) 
{ 
    $con = mysql_connect("localhost","root",""); 

    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("db_name", $con); 

    $users_name = $_POST['name']; 
    $users_email = $_POST['email']; 
    $users_contact = $_POST['contact']; 
    $users_tour = $_POST['tour']; 
    $users_location = $_POST['location']; 
    $users_comment = $_POST['comment']; 

    $users_name = mysql_real_escape_string($users_name); 
    $users_email = mysql_real_escape_string($users_email); 
    $users_contact = mysql_real_escape_string($users_contact); 
    $users_tour = mysql_real_escape_string($users_tour); 
    $users_location = mysql_real_escape_string($users_location); 
    $users_comment = mysql_real_escape_string($users_comment); 

    $query = " 
    INSERT INTO `db_name`.`dbtable` (`id`, `name`, `email`, `contact`, `tour`, `location`, `comment`, `timestamp`, `articleid`) 
    VALUES (Null, '$users_name', '$users_email', '$users_contact', '$users_tour', '$users_location', '$users_comment', CURRENT_TIMESTAMP);"; 

    mysql_query($query); 

    $url = 'thank_you.php'; 
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; 

    mysql_close($con); 

    include("mail.php"); 
} 
?> 
+0

嘗試wrtie一些驗證代碼,這樣我們就可以,如果幫你你不能讓它工作 – gbestard

+0

http://www.w3schools.com/js/js_form_validation.asp –

+0

你也必須在PHP中驗證你的表單,而不僅僅依賴於JS驗證。 –

回答

0

提交表格如下所示之前執行使用JavaScript的驗證。首先給ID到表單,所以你可以很容易地通過JS提交

<form method='post' action="database.php" id='frm'></form> 
make a call to validate function from the submit button like this 

<input type='submit' value='Button' onclick=javacript:validate(); /> 


<script language=javascript> //easy one by one validation. this allow full control 
    function validate(){ 
     if (document.getElementById('name').value==''){ 
      alert('name is required');//customize this for your need. You may use div display 
      return; 
     }       
      //repeat the above for each form tags and values 

     document.getElementById('frm').submit();//submit the form 
    } 

</script> 

有更先進的JQuery plugin進行驗證,上面是簡單的原始方法

+0

謝謝,我做了相應的你指導,驗證工作.... – user3423283