2011-12-16 181 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