JQuery.post方法不起作用

时间:2014-04-21 19:30:26

标签: javascript php jquery html post

该页面未重定向到signup.php。如果我指定表单操作,代码工作正常,但如果使用jquery则不起作用。

以下是HTML: 定义为signup.html

<head>
  <title>Pitt Research World - Signup</title>
  <link rel="shortcut icon" href="images/favicon.ico">
  <link href="style/style.css" type="text/css" rel="Stylesheet" />
  <meta charset="UTF-8" />
  <script src="js/jquery.js"></script>
  <script src="js/signup.js"></script>
</head>

<body>
<div id= "main">
<header>
        <img id = "pitt" src="images/pitt.png" height="75"/>
        <img src="images/logo.png" height ="75" alt="LOGO">
</header>
<section>
    <div class="clearfix">
    <div id="contentsMain">
            <h3 class="heading">Sign up for your new account.. </h3>
        <form method="post">
        <b>Choose the type of role you desire in the application:</b><br>
<input class="role" type="radio" name="role" value="Researcher">Researcher<br>
    <input class="role" type="radio" name="role" value="Participant">Participant<br>
    <b>Enter your First Name:</b><br>
<input type="name" name="fname" id="fname" required /><br><br>
<b>Enter your Last Name:</b><br>
<input type="name" name="lname" id="lname" required /><br><br>
<b>Enter your Email address:</b><br>
<input type="email" name="email" id="email" required /><br><br>
<b>Choose your password:</b><br>
<input type="password" name="password" id="password" required /><br><br>
<input class="subutton" type="submit" id="submitbutton" value="Create Account"/>
        </form>
    </div>
</div>
</section>
<footer>
        <p>Copyright &copy; 2014 Web Tech project group</p>
</footer>
</div>
 </body>

这里是JQuery: 在js / signup.js中定义

$(document).ready(function(){
$("#submitbutton").click(function() {
var type = $(".role").val();
var first = $("#fname").val();
var last = $("#lname").val();
var address = $("#email").val();
var pwd = $("#password").val();
$.post("php/signup.php", {role: type, fname: first, lname: last, email: address, password: pwd});
});
});

这是PHP的: 在php / signup.php中定义

<?php
$database = "pittresearchworld";
$server = "localhost";
$db_user = "root";
$db_pass = "";
$link = mysql_connect($server, $db_user, $db_pass);
mysql_select_db($database);
session_start();

$role =$_POST['role'];
$checkr = "SELECT * from researcher_login where email= '$email'";
$checkp = "SELECT * from participant_login where email= '$email'";
$qryr = mysql_query($checkr);
$qryp = mysql_query($checkp);
$num_r = mysql_num_rows($qryr);
$num_p = mysql_num_rows($qryp);

if($num_r>0 || $num_p>0){
echo "This email already exists in our database! Please try another username.";
echo '<a href="../signup.html">Try Again</a>';
}

elseif(isset($role) && isset($_POST['fname']) && isset($_POST['lname']) &&      isset($_POST['email']) && isset($_POST['$password'])) {

if($role=='Participant') {
    addParticipant();
    //header("location:sample.com");
    }
elseif($role=='Researcher'){
    addResearcher();
    //header("location:../r_list_researches.html");
}
}

function addParticipant() {
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email =$_POST['email'];
    $password = $_POST['password'];
    $sql = "insert into participant_login (firstname,lastname,password,email) values ('$fname','$lname','$password','$email')";
    mysql_query($sql);
    //$msg = ($error = mysql_error())?$error:"data added";
    //print "$msg";
     $_SESSION['user']=$email;
    }

function addResearcher() {
     $fname = $_POST['fname'];
     $lname = $_POST['lname'];
     $email =$_POST['email'];
     $password = $_POST['password'];
     $s = "insert into researcher_login (firstname,lastname,password,email) values ('$fname','$lname','$password','$email')";
     mysql_query($s);
     //$msg = ($error = mysql_error())?$error:"data added";
     //print "$msg";
      $_SESSION['user']=$email;
    }

数据库连接和表格是正确的,我已经交叉验证了它们。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试

  $.ajax({
        type: 'POST',
        url: 'php/signup.php',
        data: '{role: type, fname: first, lname: last, email: address, password: pwd}',
        success: function(data) { alert('data: ' + data); },
        contentType: "application/json",
        dataType: 'json'
    });