在HTML中,带参数的javascript函数无法传递给jQuery和ajax

时间:2014-05-17 05:53:09

标签: javascript jquery html ajax

我有简单的HTML登录代码,我在其中传递formIDdivID作为javascript&的参数jQuery函数,但它不起作用。

<?php
session_start();

<html>
    <head>
    <script src="js/jquery.min.js"></script>

    <script type="text/javascript">

        function formToResultSwitch(formID,divID){
            $.ajax({ 
                type:"POST",
                url:"connection.php",
                data: $(formID).serialize(),
                success: function(response){$(divID).html(response);}
            }); //return false;

        }
    </script>

    </head>
    <body>
    <div id="result">
        <form method="post" id="form1" action="" onSubmit="javascript:formToResultSwitch('#form1','#result')">
            <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                <tr>
                    <td colspan="3"><strong>Member Login </strong></td>
                </tr>
                <tr>
                    <td width="78">Username</td>
                    <td width="6">:</td>
                    <td width="294"><input name="username" type="text" id="username"/></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td>:</td>
                    <td><input name="password" type="text" id="password"/></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>                 
                    <td><input type="submit" value="Submit"/></td>
                </tr>
            </table>    
                </form>
             </div>
          </body>
    </html>

如果我将#form1#result直接放在功能中,而不是完美无缺。但是如果我通过它,则值抛出参数而不是使用ajax部分。如何在此函数中传递值抛出参数?

Connection.php代码:

<?php
session_start();

if(isset($_POST['username'])) {                     
    $_SESSION['user'] = $_POST['username'];
    $_SESSION['pass'] = $_POST['password'];
    $use = $_SESSION['user'];
    $pas = $_SESSION['pass'];
    echo "session variable stored";
    echo $use." ".$pas; 

    $data = explode('.',$_SERVER['SERVER_NAME']); // Get the sub-domain here
    $username = $_SESSION['user']; 
    $password = $_SESSION['pass'];

    if (!empty($data[0])) {
        $subdomain = $data[0];// The * of *.mydummyapp.com will be now stored in $subdomain

        echo "<html><head></head><body><table>";
        echo "<tr>$subdomain</tr>"; 
    }

    $mhost = 'localhost';
    $muser = 'root';
    $mpassword = '';
    $mdatabase = 'test1';
    $sitedbh = mysqli_connect($mhost, $muser, $mpassword, $mdatabase);

    if(!$sitedbh){ echo "<tr>connection prob</tr>"; }
    else {

        $qr = "select id, username, dbusername, dbpassword, dbid from clients where subdomain='$subdomain' and username='$username' and password='$password'";
        $sitemysql = mysqli_query($sitedbh, $qr)
            or die("error: " . mysqli_error());

        $appdata = mysqli_fetch_array($sitemysql);
    }

    if (empty($appdata)) {
        // If the client does not exist i.e. the end-user types the wrong sub-domain
        echo "Oops! Sorry, we are unable to find you! Please email us at support@mydummyapp.com";
        exit();
    }

    $host = 'localhost';
    $user = $appdata[2];
    $password = $appdata[3];
    $database = 'test1'.$appdata[4];

    $dbh = mysqli_connect($host, $user, $password, $database);

    if(!$dbh) { echo "<tr>connection prob</tr>"; }
    else {
        $result = mysqli_query($dbh, "SELECT role FROM table1 where username='$appdata[1]'") or die("error: " . mysqli_error());

        while ($row = mysqli_fetch_array($result)) {
            $data = $row[0];
            echo $data;
        }
    }
}
else
{
    echo "session not on";
}
?>

1 个答案:

答案 0 :(得分:0)

<强> jquery的:

function formToResultSwitch(formID,divID) {
    $.ajax({
        type: "POST",
        url: "connection.php",
        data: $(formID).serialize(),
        success: function(response) {
            $(divID).html(response);
        }
    });            
    return false; // return false to stop the default submit functionality
}

return formToResultSwitch('#form1', '#result')添加了这个,因为如果函数返回了我们需要在调用它之前返回的内容。

<强> HTML:

<form method="post" id="form1" action="" onSubmit="return formToResultSwitch('#form1', '#result')">