简体   繁体   English

表单重新加载页面而不发送提交数据

[英]Form reloading page without sending the data on submit

here's my code. 这是我的代码。

In my .js file: 在我的.js文件中:

function Sendit()
{
    bValidate = validateField();
    if(bValidate)
    {
        var title = $("#title").val();

        theUrl = 'index.php';

        params = '';
        params += 'action=Send';   
        params += '&title='+title;        
        $.ajax ({
            url: theUrl,
            data: params,
            async:true,
            success: function (data, textStatus)
            {
                //do smth
                alert('went well'); 

            }
            ,
          error: function(jqXHR, textStatus, errorThrown) 
          {
            alert(errorThrown);
          }
        }); 
    }
}

function validateField()
{
        var title = document.getElementById('title').value;         
        if(!title.match(/\S/)) 
        {
            //do some alerting 
            return false;
        }
        else
        {
         return true;
        }
}

And in my index.php file: 在我的index.php文件中:

<form action="" method="post" name="myform" id="myform"" > 
Title:  <input class="" type="text" name="title" value="" id="title"/> <br>
<input type="submit" value="Submit"  onClick="javascript:Sendit();return false; "> 
</form>

<?php
if ($_REQUEST["action"]=='Send')
{
  $title = $_REQUEST["title"];  


        $sql = "INSERT INTO ...
       $retval = $mysqli->query($sql, $conn);

       if(! $retval ) {
          echo('Could not enter data insert: ' . mysql_error());
       }
       else
       {
       //inform that everything went well 

       }
       ?>

This does not send a thing when the sunmit button is clicked. 单击sunmit按钮时,这不会发送任何内容。 In fact, you can click the button until the end of the day that nothing happens (not even a message in the debugger) 实际上,您可以单击按钮直到当天结束时没有任何反应(甚至调试器中的消息都没有)

If I delete the return false; 如果我删除了return false; from the onClick in the button, I click on the button and the page reloads even without filling in the title input which has to be filled in. 从按钮上的onClick ,我点击按钮,页面重新加载,甚至没有填写必须填写的标题输入。

Ajax's success does not alert a thing and in both cases, nothing gets inserted in my database. Ajax的成功并没有提醒一件事情,在这两种情况下,我的数据库中都没有插入任何内容。

The insert query is correct, I've checked it. 插入查询是正确的,我已经检查过了。

Any ideas on how to send the data and validate? 有关如何发送数据和验证的任何想法?

Thanks 谢谢

your validateField() function never returns true, so your if(bValidate) will never run. 您的validateField()函数永远不会返回true,因此您的if(bValidate)将永远不会运行。 Javascript functions return undefined unless you explicitly return something, try this: Javascript函数返回undefined,除非你明确地返回一些东西,试试这个:

function validateField()
{
    var title = document.getElementById('title').value;         
    if(!title.match(/\S/)) 
    {
        //do some alerting 
        return false;
    } esle {
        return true;
    }
}

Use below Code to send req. 使用以下代码发送req。

function Sendit()
{
bValidate = validateField();
if(bValidate)
{
    var title = $("#title").val();

    theUrl = 'index.php';

    params = {};
    params["action"] = 'Send';   
    params["title"] = title;        
    $.ajax ({
        url: theUrl,
        data: params,
        async:true,
        success: function (data, textStatus)
        {
            //do smth
            alert('went well'); 

        }
        ,
      error: function(jqXHR, textStatus, errorThrown) 
      {
        alert(errorThrown);
      }
    }); 
  }
}

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

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