最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery Post + run php file > hide the form > give message - Stack Overflow

programmeradmin5浏览0评论

here is my code about jquery post. I can't make it work somehow. I spent hours :( what I miss here?! when I run the code, It loads same page :(

I want it to run the php code under query.php and hide the contact form and give "thanks!" message at send submit button click. (with no page loading)

appreciate helps!


PHP Form

<form id="mentForm" name="contact" method="post" action="">
    <ul id="contact-form">
        <li><label>Full Name: *</label><input type="text" name="full_name" class="txt_input required" /></li>
        <li><input type="submit" value="Send" id="btnsend" name="btnsend" class="btn_submit" /></li>
    </ul>
</form>

SCRIPT

$(function() {

    $("#btnsend").click(function() {
        var dataString = 'fullname='+ escape(full_name);
        $.ajax({
            type: "POST",
            url: "query.php?act=contact",
            data: dataString,
            success: function() {
            $('#contact-form').hide();
        $('#contact-form').html("<p>thanks!</p>")
        .fadeIn(1500, function() {$('#contact-form').append("");});
       } 

    });

    return false;
    });
 });

here is my code about jquery post. I can't make it work somehow. I spent hours :( what I miss here?! when I run the code, It loads same page :(

I want it to run the php code under query.php and hide the contact form and give "thanks!" message at send submit button click. (with no page loading)

appreciate helps!


PHP Form

<form id="mentForm" name="contact" method="post" action="">
    <ul id="contact-form">
        <li><label>Full Name: *</label><input type="text" name="full_name" class="txt_input required" /></li>
        <li><input type="submit" value="Send" id="btnsend" name="btnsend" class="btn_submit" /></li>
    </ul>
</form>

SCRIPT

$(function() {

    $("#btnsend").click(function() {
        var dataString = 'fullname='+ escape(full_name);
        $.ajax({
            type: "POST",
            url: "query.php?act=contact",
            data: dataString,
            success: function() {
            $('#contact-form').hide();
        $('#contact-form').html("<p>thanks!</p>")
        .fadeIn(1500, function() {$('#contact-form').append("");});
       } 

    });

    return false;
    });
 });
Share Improve this question edited Sep 28, 2009 at 11:53 munity wiki
7 revs, 5 users 70%
designer-trying-coding 3
  • That <label> element is rather useless. There is no for attribute and no input inside it. – Quentin Commented Sep 28, 2009 at 11:48
  • 1 @David: the Label element is not supposed to contain the input, nor does it need a for attribute. – simon Commented Sep 28, 2009 at 11:50
  • See w3/TR/html4/interact/forms.html#h-17.9.1 — if you have a for attribute, the label is associated with the element that has an id that matches the for attribute. Otherwise, the label is associated with its contents. In this case, the label isn't associated with any form control, which makes it useless. – Quentin Commented Sep 28, 2009 at 12:30
Add a ment  | 

6 Answers 6

Reset to default 2

Your problem lies in: var dataString = 'fullname='+ escape(full_name);

Try: var dataString = 'fullname='+ escape(document.contact.full_name.value);

Eg:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en">
<head>
 <title>Example</title>

 <script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.3.2/jquery.min.js"></script>
 <script type="text/javascript">
  $(function() {
   $("#btnsend").click(function() {
    var dataString = 'fullname='+ escape(document.contact.full_name.value);

    $.ajax( {
     type: "POST",
     url: "query.php?act=contact",
     data: dataString,
     success: function() {
      $('#contact-form').hide();
      $('#contact-form').html("<p>thanks!</p>").fadeIn(1500, function() { 
       $('#contact-form').append("");
      });
       }  
    });

    return false;
   });
  });
 </script>
</head>

<body>
 <form id="mentForm" name="contact" method="post" action="">
  <ul id="contact-form">
   <li><label>Full Name: *</label><input type="text" name="full_name" class="txt_input required" /></li>
   <li><input type="submit" value="Send" id="btnsend" name="btnsend" class="btn_submit" /></li>
  </ul>
 </form>
</body>

Make sure query.php exists though else it won't execute the call back function at success.

Also make sure you click the button and not press the ENTER key as that will submit the form normally (you have only defined an event handler for click not keypress)

You can preventDefault on the button click or return false on the form's submit event:

$(function() {
    $("#btnsend").click(function(e) {
            e.preventDefault();
            var full_name = $('input["name=full_name"]').val();
            var dataString = 'fullname='+ full_name;
            $.ajax({
              type: "POST",
              url: "query.php?act=contact",
              data: dataString,
              success: function() {
                    $('#contact-form').hide();
                    $('#contact-form').html("<p>thanks!</p>")
                                      .fadeIn(1500, function() {$('#contact-form').append("");});
               }
        });
    });
});

or:

$('#mentForm').submit(function() {
    return false;
});

Try:

$(function() {

$("#btnsend").click(function() {

        $.ajax({
          type: "POST",
          url: "query.php?act=contact",
          data: { fullname: $('input[name=full_name]').val() },
          success: function() {
        $('#contact-form').hide();
                $('#contact-form').html("<p>thanks!</p>")
        .fadeIn(1500, function() {$('#contact-form').append("");});
           } 

    });

    return false;
});
});

I think that you have at least 3 problems here. First, you are referencing full_name as if it were a variable. I believe this is causing a javascript error which aborts the function and allows the default action (post) to proceed. Second, you probably need to encode ALL of the form parameters, including act. This is may be causing an invalid URL to be sent and thus the action appears not to have been invoked -- you can check this by looking at the request that is sent with Firefox/Firebug. Third, you are attempting to replace the contents of a list with a paragraph element. This is invalid HTML. I'd replace the entire list with the paragraph (and I don't get what appending an empty string does at the end so I've omitted it).

Note I've changed this to work on the submit event of the form -- so it won't matter how it's submitted. Also allows me a little jQuery niceness in not having to look up the form again.

 $('#mentform').submit(function() {
   var $this = $(this);
   $.ajax({
     url: "query.php",
     data:  { 'act': 'contact', 'full_name', $('input[name="full_name"]').val() },
     success: function() {
        $('#contact-form').remove();
        $this.hide().html("<p>thanks!</p>").fadeIn(1500);
     }  
  });
  return false;
}

This is my way to call PHP function directly via jQuery

// in html file
<script language="javascript">
  $.post("thisisphp.php",{ func:"true", varbl: 'valu2' },function(data) {
    $('.thisdiv #subdiv').html(data);
  });    
</script> 

PHP file thisisphp.php

<?php
    // include connection creating file.
    require_once("database.php");
    $db = & new Database();

    function getDateAppointments($varible1, $varible2, $db) {    
      $q_appointment = "SELECT * FROM `tbl_apoint` WHERE ap_date = '".$varible1."'"; 
      $s_appointment = $db->SelectQuery($q_appointment);

      while($r_appointment = mysql_fetch_array($s_appointment))
      {
        echo '<div id="appoinment">'.$r_appointment['ap_title'].'</div>'; 
      }     
    }    

    /* This function for set positions of the day */        
    switch($_POST['func']) {
      case 'true':
        getFunc1($_POST['varbl'], 'S0001', $db);
        break;
      default:
        getFunc2($_POST['varbl'], 'S0001', $db);        
    }    
?>

Can this help you?

PS: you can put your hidding and showing form script inside onSucces and onError functions.

发布评论

评论列表(0)

  1. 暂无评论