【问题标题】:don't submit form if form is empty如果表单为空,请不要提交表单
【发布时间】:2013-05-21 00:34:42
【问题描述】:

这是我的第一篇文章,我对 php + ajax 比较陌生。

我的大部分编码设置现在唯一的问题是当我按下提交按钮时会显示一个通知,该通知仅在文本字段不为空时才会显示。

这是我的代码:

<head><script> 
    $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#ContactForm').ajaxForm(function() { 
            alert("Thank you for subscribing to SHCA"); 
            document.forms["ContactForm"].reset();
        }); 
    }); 

</script> </head>

后面是一些php编码:

<?php
        if(strlen($_POST['name']) > 0 AND strlen($_POST['email']) > 0)
        {
        if(isset($_POST['submit']))
        {
         $hostdb = '';
         $namedb = '';
         $userdb = '';
         $passdb = '';

        $conn = mysqli_connect($hostdb , $userdb, $passdb ,$namedb);


        $sql = "INSERT INTO subscribers (name, email) VALUES('$_POST[name]', '$_POST[email]')";



        if (!mysqli_query($conn, $sql))
        {
        die('Error: ' .mysql_error($conn));
        }

        if (!mysqli_ping($conn)) {
echo 'Lost connection, exiting after query #1';
exit;
}

mysqli_close($conn);
}}
else
{

?>

        <form id="ContactForm" action="" method="post">

                    <label for="author">Name:</label> <input type="text" id="name" name="name" class="required input_field float_r" />
                    </br>
                    </br>
                    <label for="email">Email:</label> <input type="text" id="email" name="email" class="validate-email required input_field float_r" />

                    <div class="cleaner h10"></div>

                    <input type="submit" value="Subscribe" id="submit" name="submit" class="submit_btn float_l" />
        </form>
        <?php } ?>

希望这里的任何人都能够告诉我,当文本字段不为空时,我应该怎么做才能只显示“感谢您订阅 SHCA”消息

最终回答者 Dereck 忘记了对象中的“”,所以大声呼喊 dereck 帮助我!

$(document).ready(function() { 
    // bind 'myForm' and provide a simple callback function 
    $('#ContactForm').ajaxForm(function() { 
        if( !$('#name').val()) {
             alert("please enter your name");
        }
        if(!$('#email').val()) {
            alert("plese enter your email adress");
            }
        else{
            alert("Thank you for subscribing to SHCA"); 
        }

        document.forms["ContactForm"].reset();
    }); 
}); 

【问题讨论】:

  • 我很好奇的是,我是否可以防止 sql 注入,如果没有,我需要做些什么来保护自己免受它的侵害。

标签: php database jquery textfield formfield


【解决方案1】:

在提交到服务器之前需要检查字段的内容,一个简单的脚本方法可以在提交之前检查。如果字段为空,则显示错误消息并且不要提交表单。

function SubmitDetails()
{
    if(window.ContactForm.name.value == "")
    {
        window.alert("Please enter a name");
        return;
    }if(window.ContactForm.email.value == "")
    {
        window.alert("Please enter an email" );
        return;
    }

    window.ContactForm.submit();
} 

【讨论】:

  • 我应该在哪里植入它?我需要把 ajax 拿出来还是它的一个补充
  • 这个应该放在客户端,当提交按钮被按下时,你的脚本会在发送到服务器之前进行拦截。 onclick='SubmitDetails();'
  • 我明白你现在的意思了只要是用来提交表单的
  • 那么您是否尝试提交并添加详细信息而不实际提交页面?不想刷新?
  • 不,不希望它刷新,它现在的工作方式完美无缺,除了我收到“您现在订阅了”消息,即使您只是将字段留空
【解决方案2】:

这应该不用刷新就可以了

$(document).ready(function() { 
    // bind 'myForm' and provide a simple callback function 
    $('#ContactForm').ajaxForm(function() { 
        if( !$(#name).val() || !$(#email).val()) {
             //don't display
        }else{
            alert("Thank you for subscribing to SHCA"); 
        }

        document.forms["ContactForm"].reset();
    }); 
}); 

【讨论】:

  • 您是否知道我只能在 ajax 中检查 1 个字符?所以就像你在上面向我展示的那样,然后我想检查电子邮件字段中是否有@
猜你喜欢
  • 2013-04-15
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多