基于单选按钮选择提交Ajax运行给定查询

时间:2011-11-11 03:10:46

标签: php ajax login radio-button

HTML:

<form style="margin:5px 0;" action="#" method="post"> 
            Buyer <input type="radio" name="addType" value="Buyer" />
            &nbsp;&nbsp;Merchant <input type="radio" name="addType" value="Merchant" />
        </form>
        <form id="NewBuyerRegHp" method="post" action="check.php">
            Username or Email: <input type="text" name="userOrEmail" class="UserLogin" value="Username" onFocus="clearText(this)" /> <br />
            Password: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="userPass" class="UserLogin" value="Password" onFocus="clearText(this)"  /> <br />
            <input type="submit" name="SubmitNewBuyerHp" value="Secure Login" />
        </form>

PHP    

$NewBuyerLoginCheck = mysql_query("SELECT uUName, uEmail, uUPass, dynamSalt FROM User WHERE uUName OR uEmail = '".mysql_real_escape_string($_POST['userOrEmail'])."' AND uUPass = sha2(concat(dynamSalt, '".mysql_real_escape_string($_POST['userPass'])."'), 512) ")or die(mysql_error());

$NewMerchLoginCheck = mysql_query("SELECT mUsername, mEmail, mUPass, mdynamSalt FROM Merchant WHERE mUsername OR mEmail = '".mysql_real_escape_string($_POST['userOrEmail'])."' AND mUPass = sha2(concat(dynamSalt, '".mysql_real_escape_string($_POST['userPass'])."'), 512) ")or die(mysql_error());


if(mysql_num_rows == 1){
echo "Success"; 
//then redirect the users homepage
}
else(mysql_num_rows == 0){
echo "Error"; 
// then redirect to original login form, for relogin
}

当选择单选按钮:value="Buyer"时,它应运行查询$NewBuyerLoginCheck

当选择单选按钮:value="Merchant"时,它应运行查询$NewMerchLoginCheck

如何根据使用Ajax提交时选择的单选按钮执行此操作?

2 个答案:

答案 0 :(得分:0)

使用此示例

    $('input:radio').click(function(){
if($("input:checked").val() == "Buyer")
{
    //ajax call to php file and pass a parameter buyer
}
else{
    //ajax call to php file and pass a parameter merchant        
}
});

并获取php代码的post值,并根据传递的参数执行查询

答案 1 :(得分:0)

html代码如果你使用jQuery,你可以这样做:

<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){           
            $('#NewBuyerRegHp').submit(function() {
                $.ajax({
                    type: 'POST',
                    url: $(this).attr('action'),
                    data: $(this).serialize(),
                    success: function(data) {
                        $('#ajax').html(data);
                    }
                })
                return false;
            });
        });
</script>
<form id="NewBuyerRegHp" method="post" action="check.php" style="margin:5px 0;">
    Buyer <input type="radio" name="addType" value="Buyer" checked="checked" /> <!-- Default value is checked -->
    Merchant <input type="radio" name="addType" value="Merchant" /><br />
    Username or Email: <input type="text" name="userOrEmail" class="UserLogin" placeholder="Username" /> <br />
    Password: <input type="password" name="userPass" class="UserLogin" placeholder="Password"  /> <br />
    <input type="submit" name="SubmitNewBuyerHp" value="Secure Login" />
</form>
<div id="ajax"></div> <!-- Check return value if using ajax -->

Php代码:

<?php
if (isset($_POST['addType']) && isset($_POST['userOrEmail']) && isset($_POST['userPass'])){
    if($_POST['addType'] == 'Buyer'){
        $NewBuyerLoginCheck = mysql_query("SELECT uUName, uEmail, uUPass, dynamSalt FROM User WHERE uUName OR uEmail = '".mysql_real_escape_string($_POST['userOrEmail'])."' AND uUPass = sha2(concat(dynamSalt, '".mysql_real_escape_string($_POST['userPass'])."'), 512) ") or die(mysql_error());
    } elseif($_POST['addType'] == 'Merchant'){
        $NewMerchLoginCheck = mysql_query("SELECT mUsername, mEmail, mUPass, mdynamSalt FROM Merchant WHERE mUsername OR mEmail = '".mysql_real_escape_string($_POST['userOrEmail'])."' AND mUPass = sha2(concat(dynamSalt, '".mysql_real_escape_string($_POST['userPass'])."'), 512) ") or die(mysql_error());
    }
    if(mysql_num_rows == 1){
        echo "Success";
        //then redirect the users homepage
    } elseif(mysql_num_rows == 0){
        echo "Error";
        // then redirect to original login form, for relogin
    }
}
?>