0

I have a basic form, I only want it execute when the users input matches a set one. How can I make it so the user can only get to the add books page if the supply the "Admin" username and the password "password1"?

//Admin Login
echo "<form method='post' action='addBooks.php'>
Username:<br>
<input type='text' name='Username' value='Admin'>
<br>
Password:<br>
<input type='text' name='password' value=''>
<br><br>
<input type='submit' name='sbmt' id='sbmt' value='user_value' class='user_class' onSubmit='return submit();'/> 
</form>";

function submit(){
            if($_POST['Username'] == 'Admin')&&($_POST['password'] == 'Password1'){
                return true;
           }
           else{
               alert("Please check Username and Password")
               return false;
           }
    }

Edit:

To clear up the issue.

I am trying to have a form with a username and password input. When the user clicks submit on this form it currently takes the user to addbooks.php. I want to make this conditional so that the user can only access the addbooks.php page if the username they provide ="Admin" and the password the provide = "Password1". I am currently trying to execute the submit function on the button click and the submit function is supposed to check if username and password match admin and password1. if they do it should let you through to addbooks.php

7
  • 1
    This doesn't work? Are you trying to call a PHP function with JavaScript? Commented Mar 17, 2015 at 21:32
  • execute what? which is your problem? Commented Mar 17, 2015 at 21:34
  • I guess that is the problem, but i am not sure how to get around it. The method I am using is from a solution found here on SO (i modified for my purposes) that doesn't seem to work at all for my code. Commented Mar 17, 2015 at 21:35
  • @AdrianCidAlmaguer Clarified main post. Commented Mar 17, 2015 at 21:39
  • PHP executes before the JS. It isn't available once the page has loaded. You could send this through an ajax request to another PHP script that will check the pass and username or check if the POST has been submitted and then check your values presuming this page is 'addBooks.php'. Commented Mar 17, 2015 at 21:39

1 Answer 1

1

You could try this (note the header("Location: addBooks.php") part MUST be at the top of your code, before any html is outputed):

if  (isset($_POST["Username"]) and isset($_POST["password"]) ) {

    if ( ($_POST["Username"] == "Admin") and ($_POST["password"] == "Password1") ) {        
        header("Location: addBooks.php");
        exit;   
    }
    else {echo "Please check Username and Password <br/>";}
}


//Admin Login
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">
Username:
<br/>
<input type="text" name="Username" value="Admin">
<br/>
Password:
<br/>
<input type="text" name="password" value="">
<br/>
<br/>
<input type="submit" name="sbmt" id="sbmt" value="Submit"    class="user_class"/> 
</form>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.