在 jQuery 中提交表单后,在 PHP 中完成

发布于 2024-12-21 19:28:53 字数 770 浏览 0 评论 0原文

我有一个 php 文件,它包含一个文本文件和提交按钮以及一个 div,下面是代码

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它插入数据库了吗?

I have a php file and it contains a textfiled and submit button and a div and below is code

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
  }
}

And the jQuery code:

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

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

Question: How can I show/display that suc_box when form is submitted after it inserted into database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-12-28 19:28:53

您可以使用 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 (!/^\S+@\S+\.\w+$/.test(document.sweetform.Email.value)) {
            alert("Not a valid e-mail address");
            return false;
        }
        else {
            return true;
        }

    }

You can do it easily with AJAX. You have one php-file with the form and another one for processing the data:

//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!';
}

It's untested, but you get the idea.

validating email field

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

    }
放肆 2024-12-28 19:28:53

如果我猜对了,你可以这样做,但我建议你研究一下 ajax 来做你想做的事

    <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
    }

    }
?>

If ive got you right you could do this however I would suggest looking into ajax to do what you want

    <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
    }

    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文