Reputation: 755
So I have a form
<form id="login_form" method="post" action="login.php">
<input type="text" name="email">
<input type="password" name="pass">
<input type="submit" name="submit" value="Log in" onclick="submitFunction()">
</form>
When the form is submitted I am checking if the username and password are correct
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "SELECT email,password FROM user_details WHERE email='$email' AND password='$pass'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
}
else {
echo "Incorrect username or password. Please try again!";
}
}
But I also want to load a new page, which I am trying to do via javascript
function submitFunction()
{
window.location.href = "new url";
}
I tried replacing the submit button with just a button however then I can not get my php to execute because I can't use if(isset($_POST['submit'])), but if I use a submit button then I do not know how to call my javascript function because I can't use onclick can I? Any help would be appreciated :)
Also I know I should not just store the password in my database and sql injection and all that but I just want to try and get this to work
Upvotes: 0
Views: 1921
Reputation: 977
This is where Ajax comes in. I would perform this using AJAX and Jquery Do not forget to include Jquery.
$("#check").click(function(){
$.ajax({
type: "POST",
url: "file.php",
data: {email: $("#email").val(), pass: $("#pass").val()},
beforeSend: function()
{
$('#check').attr('disabled',true);
},
success : function(response)
{
if(response=="correct"){
setTimeout('window.location.href = "new url"',1000);
}else{
setTimeout('window.location.href = "bad login url"',1000);
}
}
});
return false;
});
In your Php code file.php Use this
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "SELECT email,password FROM user_details WHERE email='$email' AND password='$pass'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
echo "correct";
}
else {
echo "Incorrect username or password. Please try again!";
}
}
For Html
<form id="login_form" method="post">
<input type="text" id="email" name="email">
<input type="password" id="pass" name="pass">
<input type="submit" name="submit" value="Log in" id="check">
</form>
Hope this information will help Please learn Ajax, It is very helpful in such problems. Thank you
Upvotes: 0
Reputation: 948
Not really sure where you want to execute the JS code, something like this:
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "SELECT email,password FROM user_details WHERE email='$email' AND password='$pass'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
// Output JsvaScript code here?
echo "
<script>
function submitFunction()
{
window.location.href = \"new url\";
}
</script>
";
}
else {
echo "Incorrect username or password. Please try again!";
}
}
Something like that should work but there are a few security issues with your code. I'll assume you're just messing about and learning.
Upvotes: 0
Reputation: 41810
You can just load the new page with PHP after you check the login. Unless you have some other reason you need to do it with JavaScript, I think it makes more sense to do it in PHP anyway, because you'll want to do different things depending on whether or not the login was successful.
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
header('Location: new url');
exit;
}
else {
echo "Incorrect username or password. Please try again!";
}
Upvotes: 2