简体   繁体   English

表单不会通过Ajax提交

[英]Form wont submit via ajax

I have a form within a div that I want to submit via another page which will run the query and then return to the first page again all within the div which is in a larger page. 我有一个div内的表单,我想通过另一个页面提交该表单,该表单将运行查询,然后再次返回到第一页面,而所有div都位于较大页面内。

I have put the following javascript at the top of the page: 我将以下javascript放在页面顶部:

<script name='addactivity'>
    function submitForm() {
        $.ajax({type:'POST', url: 'activity_new.php', data:$('#newactivity').serialize(), success: function(response) {
            $('#newactivity').find('.activities').html(response);
        }});

        return false;
    }
    </script>

and have placed the form in the div as follows: 并将表单放在div中,如下所示:

<form id='newactivity' method="post">
        <b>Activity Number:</b><input type=text name='activitynumber' class='textborder'>   
        <b>Title:</b><input type=text name='activitytitle' class='textborder'>
        <b>Time (mins):</b> <input type=text name='activitytime' class='textborder'>
        <b>Leaders:</b> <input type=text name='leaders' class='textborder'><br>
        <b>Description:</b><textarea name='activitydescription' class='textareaborder'></textarea>
        <input type='submit' value='Submit' id='submit'></form>
<div id="activities"></div>

The submit button does not appear to do anything. 提交按钮似乎没有任何作用。 It should post the values to activity_new.php which looks like: 它应该将值发布到activity_new.php,如下所示:

<?php
session_start();
$input2=$_SESSION[ 'unitid' ];

        $meetingid=$_POST['meetingid'];
        $activitynumber=$_POST['activitynumber'];
        $activitytitle=$_POST['activitytitle'];
        $activitytime=$_POST['activitytime'];
        $leaders=$_POST['leaders'];
        $activitydescription=$_POST['activitydescription'];

include 'connect_db.php'; 

$q1c="INSERT into activities (meetingid, unitid, activitynumber, title, description, time, leaders) VALUES ('$meetingid', '$input2', '$activitytitle', '$activitydescription', '$activitytime', '$leaders')";
$r1c = mysqli_query($dbc,$q1c); 

echo $meetingid;
echo $activitynumber;
echo $activitytitle;

//header("location:editmeeting.php?id=$input2");

?>

I currently have the javascript on the main page that contains the divs but have also tried it at the top of the page with the form in. I've also tried these two combinations with the onsubmit = "return submitForm();" 目前,我在包含div的主页上使用了javascript,但还在包含表单的页面顶部对其进行了尝试。我还使用onsubmit = "return submitForm();"尝试了这两种组合 as onclick = "return submitForm();" onclick = "return submitForm();" on the button itself. 在按钮本身上。

as per your code when you click on button it will not fire onsubmit() event . 根据您的代码,当您单击按钮时,它不会触发onsubmit()事件。

if you change button type to submit it will work as you have added return false to your function 如果您更改按钮类型以提交,它将在您添加功能时return false

<input type='submit' value='Submit'>

Or use below code .you can use button click event to submit form via ajax using jquery 或使用下面的代码。您可以使用按钮单击事件使用jquery通过ajax提交表单

HTML HTML

<input type='button' value='Submit' id="submit">

Jquery jQuery的

    $(document).ready(function(){
   $(document).on('click','#submit',function(){

     $.ajax({type:'POST', url: 'activity_new.php', data:$('#newactivity').serialize(), success: function(response) {
        $('#newactivity').find('.activities').html(response);
    }}); 

  });
  }); 

NOTE : from below code in success function . 注意:从下面的代码可以成功运行。 there is no element named '.activities' in your form. 您的表单中没有名为'.activities'元素。 make sure to use correct selector attribute. 确保使用正确的选择器属性。

 $('#newactivity').find('.activities').html(response);

Simply change input type: 只需更改输入类型:

<input type='submit' value='Submit'>
             ^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM