2011-12-16 179 views
0

完成我有一個PHP文件,它包含一個textfiled和提交按鈕和一個div及以下代碼形式提交後jQuery的PHP中

page1.php中

<form name="form1" method="post"> 
    <input type="text" name="email1"> 
    <input type="submit" name="submit" value="send" class="submit_class"> 

    <div class="suc_box">You have Entered</div> 
</form> 

if($_POST['submit']) { 
    $v1 = $_POST['email1']; 

    // $query1 = here some code to insert into database 

    if($query1 > 0){ 
    //here i want to display the div `suc_box`.. Here how i can show that div 
    } 
} 

而jQuery代碼:

$(document).ready(function(){ 
    $('suc_box').hide(); 

    $('suc_box').click(function(){ 
     $(this).hide(); 
    }); 
}); 

問:我怎樣才能顯示/顯示器suc_box時後,插入數據庫表單提交?

+1

哪裏是你的`?`? – wong2 2011-12-16 13:33:45

+0

感謝您提供代碼,但您的問題是什麼? – 2011-12-16 13:34:37

回答

3

你可以用AJAX輕鬆地做到這一點的。你有一個PHP文件的形式和另一個用於處理數據:

//form_file.php

<form id="my_form" onsubmit="validateform();"> 
    <input type="text" name="email1" /> 
    <input type="submit" value="OK" /> 
</form> 

<div class="suc_box"></div> 

<script> 
$(document).ready(function(){ 
    $('.suc_box').click(function(){ 
    $(this).hide(); 
    }); 

    $('#my_form').submit(function(){ 
    var data = $(this).serialize(); 
    $.post('process.php',data,function(return_data){ 
     $('.suc_box').html(return_data);   
    }); 
    return false; //cancel the 'real' submit 
    }); 
}); 
</script> 

//process.php

<?php 
$email = mysql_real_escape_string($_POST['email1']); 
//write data to DB 
if($succeeded) { 
    echo 'You have Entered'; 
} else { 
    echo 'Something went wrong, try again!'; 
} 

這是未經測試,但你明白了。

驗證電子郵件字段

function validateform(){ 
     if (!/^\[email protected]\S+\.\w+$/.test(document.sweetform.Email.value)) { 
      alert("Not a valid e-mail address"); 
      return false; 
     } 
     else { 
      return true; 
     } 

    } 
0

如果香港專業教育學院得到了你的權利,你可以做到這一點,但是我會建議尋找到阿賈克斯,做你想做

<div class="suc_box"> 
     You have Entered 
    </div> 
    </form> 
    <?php 
    if($_POST['submit']) 
    { 
    $v1 = $_POST['email1']; 

    // $query1 = here some code to insert into database 

     if($query1 > 0){ 
     ?> 


     <script type="text/javascript"> 
      $('suc_box').show(); 

     </script> 
<?php 
    } 

    } 
?> 
+0

不工作 – Rafee 2011-12-16 13:57:12