如何使用input type = button提交表单

时间:2015-03-05 15:39:37

标签: javascript php html

这是我的表单,在我使用input type = submit之前,但现在我使用验证状态,这就是为什么我需要打开输入类型=按钮,我的问题是如何提交这个表格使用按钮?

// HTML CODE

<div class="panel-body" style="width:700px;">
    <form class="form-group" method="post" id="question_form">
        <div class="form-group">
            <label>Your email address<span style="color:#FF0000">*</span></label>
            <input type="text" name="email" size="80" class="form-control" id="emailq" placeholder="Enter you email address">
        </div>
    <div class="form-group">
        <label>Message<span style="color:#FF0000">*</span></label>
        <textarea rows="8" cols="50" class="form-control" name="message" id="messageq" placeholder="What is your concern?"></textarea>
    </div>
    <div class="form-group">
        <label style="width:170px;">Enter the code you see<span style="color:#FF0000">*</span></label>
        <span style="background:#CCC; font-size:20px; color:#000; padding-top:5px; padding-bottom:5px; padding-left:30px; padding-right:30px"><?=$random_text?></span> 
        <input type="text" size="80" name="uIcaptcha" class="form-control" id="codeq" placeholder="This code is for security purposes only.">
    </div>
    <div class="form-group">
        <input type="hidden" name="captcha">
        <input type="button" id="question_submit" name="submit" value="Send Request" class="btn btn-primary">
    </div>
    </form>
</div>

// JS CODE

function validateText(id){
if($("#"+id).val()==null || $("#"+id).val()=="")
{
    var div = $("#"+id).closest("div");
    div.removeClass("has-success");
    $("#gly"+id).remove();
    div.addClass("has-error has-feedback");
    div.append('<span id="gly'+id+'" class="glyphicon glyphicon-remove form-control-feedback"></span>');
    return false;
}else{
    var div = $("#"+id).closest("div");
    div.removeClass("has-error");
    div.addClass("has-success has-feedback");
    $("#gly"+id).remove();
    div.append('<span id="gly'+id+'" class="glyphicon glyphicon-ok form-control-feedback"></span>');
    return true;
}

}

$(document).ready(
function()
{
    $("#question_submit").click(function()
    {
        validateText("emailq");

        validateText("messageq");

        validateText("codeq");


        $("form#question_form").submit();

    });
}

);

// PHP代码

 if(isset($_POST['submit'])){
 $email = $_POST['email'];
 $message = $_POST['message'];
 $captcha = $_POST['captcha'];
 $user_Input = $_POST['uIcaptcha'];

$exists = mysqli_query($con,"SELECT * FROM users WHERE email = '".$email."'");
if(!empty($email) and !empty($message)and !empty($user_Input)){    //check if empty

    if(mysqli_num_rows($exists) > 0){ //check if email is valid

        if($captcha == $user_Input){ //check if captcha is correct
            echo 'Query Here';

        } else{
            $error_code = 'Please enter correct code.'; //close if captcha checking
        }

    }else{
        $error_email = 'Please enter the valid email address'; // close if email validation         
    }
}else{
    $error = 'Please fill up all information!';  //close if empty checking
}

}

2 个答案:

答案 0 :(得分:0)

function getStates(value) {   //this my code that show result 
    $.post("search.php", {name:value},function(data){
      $("#results").html(data); 
      if(!data) {
        alert("i am blank and hiding result text box");
        $("#results").hide();
      }
   }); 
 }

如果这不起作用,请告诉我。

答案 1 :(得分:0)

您只需在每个表单中插入一个隐藏按钮: 例如:

<div class="form-group">
    <input type="hidden" name="captcha">
    <input type="button" id="question_submit" name="fakeSubmit" value="Send Request" class="btn btn-primary">
     <input type="submit" name="submit" id="submit" style="display:none;"/>
</div>

然后当你点击假一个

时,只需点击实际提交按钮上的点击
jQuery('#question_submit').click(function(){jQuery('#submit).toggle('click')});

多数民众赞成我如何做到这一点 然后你可以简单地在点击真实的隐藏提交之前添加if(条件)

希望我能帮到你