简体   繁体   English

jQuery.post方法不起作用

[英]JQuery.post method not working

The page isn't redirecting to signup.php. 该页面未重定向到signup.php。 The code works fine if I specify a form action, but doesn't work if jquery is being used. 如果我指定一个表单操作,则该代码可以正常工作,但是如果正在使用jquery,则该代码将不起作用。

Here's the HTML: Defined as signup.html 这是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>

Here's the JQuery: Defined in js/signup.js 这是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});
});
});

Here's the PHP: Defined in php/signup.php 这是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;
    }

The database connection and tables are correct, I have cross-verified them. 数据库连接和表是正确的,我已经交叉验证了它们。 Any help would be appreciated. 任何帮助,将不胜感激。

Try 尝试

  $.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'
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM