0

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?

1
  • Thanks for showing the code, but what is your question? Dec 16, 2011 at 13:34

2 Answers 2

3

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

    }
7
  • Its now working $('.suc_box').html(return_data); not returning any data from process.php
    – Rafee
    Dec 16, 2011 at 14:08
  • 1
    @Rafee please make sure div is not hidden or something. You can easily debug with FireBug (addons.mozilla.org/en-US/firefox/addon/firebug) - have a look at the HTML-tab (if the text is there) and especially the Console-tab while posting (to see what data is posted and if there were errors). Or use alert(return_data); to see if something is being returned. If really nothing is returned, make sure you have set the right path and filename to process.php (in this example, both files must me in the same directory).
    – Quasdunk
    Dec 16, 2011 at 14:12
  • actually this is email field and even i want to make it validating email.. and updating the code before it sends
    – Rafee
    Dec 16, 2011 at 14:19
  • And if the validation is false, then message div also displaying You have Entered
    – Rafee
    Dec 16, 2011 at 14:21
  • 1
    @Rafee You can (or should I say: MUST) validate it on the server side, which is in process.php, before inserting into database. Everything you echo out in process.php will be returned to form_file.php and be accessible via the return_data-variable within your postback-function. So do your validation and then echo whatever you want under whatevere conditions you want.
    – Quasdunk
    Dec 16, 2011 at 14:23
0

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
    }

    }
?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.