jquerypost+;运行php文件>;隐藏表单>;传达信息

jquerypost+;运行php文件>;隐藏表单>;传达信息,php,javascript,jquery,forms,post,Php,Javascript,Jquery,Forms,Post,这是我关于jquerypost的代码。我怎么也做不到。我花了几个小时:(我错过了什么?!当我运行代码时,它加载相同的页面:( 我想让它在下面运行php代码 并隐藏联系人表单 并在发送时发送“谢谢”信息 提交按钮点击。(无页面) 装载) 感谢你的帮助 PHP表单 <form id="commentForm" name="contact" method="post" action=""> <ul id="contact-form"> <li&g

这是我关于jquerypost的代码。我怎么也做不到。我花了几个小时:(我错过了什么?!当我运行代码时,它加载相同的页面:(

我想让它在下面运行php代码 并隐藏联系人表单 并在发送时发送“谢谢”信息 提交按钮点击。(无页面) 装载)

感谢你的帮助


PHP表单

<form id="commentForm" 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>

  • 全名:*

脚本

$(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;
    });
 });
$(函数(){
$(“#btnsend”)。单击(函数(){
var dataString='fullname='+转义(full_name);
$.ajax({
类型:“POST”,
url:“query.php?act=contact”,
数据:dataString,
成功:函数(){
$(“#联系方式”).hide();
$(“#联系方式”).html(谢谢!

) .fadeIn(1500,function(){$('#contact form').append(“”;}); } }); 返回false; }); });
您可以单击按钮,或在表单的提交事件中返回false:

$(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("");});
               }
        });
    });
});
尝试:

$(函数(){
$(“#btnsend”)。单击(函数(){
$.ajax({
类型:“POST”,
url:“query.php?act=contact”,
数据:{fullname:$('input[name=full_name]')。val(),
成功:函数(){
$(“#联系方式”).hide();
$(“#联系方式”).html(谢谢!

) .fadeIn(1500,function(){$('#contact form').append(“”;}); } }); 返回false; }); });
您的问题在于:
var-dataString='fullname='+escape(fullname);

试试:
var-dataString='fullname='+转义(document.contact.full_name.value);

例如:


实例
$(函数(){
$(“#btnsend”)。单击(函数(){
var dataString='fullname='+转义(document.contact.full_name.value);
$.ajax({
类型:“POST”,
url:“query.php?act=contact”,
数据:dataString,
成功:函数(){
$(“#联系方式”).hide();
$(“#联系方式”).html(“谢谢!

”).fadeIn(1500,function(){ $(“#联系方式”)。追加(“”); }); } }); 返回false; }); });
  • 全名:*

确保
query.php
存在,否则它将不会在成功时执行回调函数


另外,请确保您单击该按钮,而不是按ENTER键,因为这将正常提交表单(您只为click not keypress定义了一个事件处理程序)我认为您在这里至少有3个问题。首先,您引用的是全名,好像它是一个变量。我认为这会导致javascript错误,该错误会中止函数并允许默认操作(post)要继续。第二,您可能需要对所有表单参数进行编码,包括
act
。这可能会导致发送无效的URL,因此操作似乎没有被调用——您可以通过查看Firefox/Firebug发送的请求来检查这一点。第三,您正在尝试替换l的内容带段落元素的列表。这是无效的HTML。我会用段落替换整个列表(我不知道在结尾追加空字符串的作用,所以我省略了它)

注意,我已将此更改为处理表单的提交事件,因此无论如何提交都无关紧要。另外,jQuery还允许我在不必再次查找表单的情况下使用它

 $('#commentform').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;
}
$('#commentform')。提交(函数(){
var$this=$(this);
$.ajax({
url:“query.php”,
数据:{'act':'contact','full_name',$('input[name=“full_name”]')。val(),
成功:函数(){
$(“#联系方式”).remove();
$this.hide().html(“谢谢!

”).fadeIn(1500); } }); 返回false; }
这是我通过jQuery直接调用PHP函数的方法

// in html file
<script language="javascript">
  $.post("thisisphp.php",{ func:"true", varbl: 'valu2' },function(data) {
    $('.thisdiv #subdiv').html(data);
  });    
</script> 
//在html文件中
$.post(“thisisphp.php”,{func:“true”,varbl:'valu2'},函数(数据){
$('.thisdiv#subdiv').html(数据);
});    
PHP文件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);        
    }    
?>

能帮你吗


PS:您可以将隐藏和显示表单脚本放在UCS和onError函数中。

该元素非常无用。其中没有for属性,也没有输入。@David:Label元素不应该包含输入,也不需要for属性。请参阅-如果您有for属性,则标签与e关联元素,该元素的id与for属性匹配。否则,标签与其内容关联。在这种情况下,标签与任何表单控件都不关联,这使其无效。+1-我认为他也会遇到问题,因为“btnSend”是一个提交按钮。@Paul Suart:啊,是的,可能。不过他返回的是'false',这可能会取消活动。哦,哦!我真不敢相信!非常感谢……我的主要内容是关于query.php访问:/n现在它会发出感谢消息,效果会逐渐消失:)现在,我将处理电子邮件操作和表单控件!谢谢!我如何使用另一个验证脚本来运行它?:/I使用链接处的脚本。可能我需要从验证脚本的成功案例中调用此post脚本?或者没有办法?如果要使用jQuery,那么也可以使用jQuery来获取输入值。此外,您还需要d在进行ajax post之前必须运行验证程序。最简单的方法是更改以处理表单上的提交事件,并确保在ajax提交处理程序之前应用验证程序的处理程序。这样,只有表单有效时才会调用函数。虽然我在验证后加载jquery-post.js,但它会在首先,是不是因为我在提交按钮点击时运行了post代码?$(document).ready(function(){$(“#commentForm”).validate();})@artmania-另外,您可以安全地删除
 $('#commentform').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;
}
// in html file
<script language="javascript">
  $.post("thisisphp.php",{ func:"true", varbl: 'valu2' },function(data) {
    $('.thisdiv #subdiv').html(data);
  });    
</script> 
<?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);        
    }    
?>