Jump to content

Html -> Php Calling Query


wongadob

Recommended Posts

I have a piece of code that currently calls a javascipt function to do a dummy test on a user name and password. I am a complete novice to Web development, but not to programming. The JS function I have written works fine and does what it should which returns true or false dependant on whether the password is correct or not. HTML then moves to the next page if the password is correct. Now I am ready to do the correct way via PHP (as usernames and passwords are stored in an SQL database). Before I type more I need to include some code so you know what I am talking about.

  <div id = "mainbody">   <form method = "post" action="Homepage.html" onsubmit="return login()">    <fieldset>	 <p>	  <br/><br/><br/>	  <label>Username </label>	  <input type ="text"		  name = "txtUserName"		  id = "txtUserName" />	  <br/><br/><br/>	  <label>Password </label>	  <input type ="password"		  name = "txtPassword"		  id = "txtPassword" />	  <br/><br/><br/>		 	  <button type = "submit"> <img src = "Art\Menu Buttons - Enter.png" alt = "Enter"/> </button>	  <br/>   	  	 </p>	 <div id = "ErrorMessageArea">	 </div>    </fieldset>   </form>  </div>

Now as you can see the line - <form method = "post" action="Homepage.html" onsubmit="return login()"> Calls my function login and on true goes to Homepage.html which is the main title page of my system once the user is logged in. But from what I can see the equivalent in PHP calling is just. <form method="post" action "xxx.php"> So it immediately transfers to my php flle. What is the correct structure to get around this. OK I believe I could embed PHP code to check the password in homepage (I think) but then I would have to return back to index.html(Username and password entry screen) if they entered incorrectly. What is the correct way of doing this. I guess I am asking structurally rather than specific instructions, but they may help my understanding too. Thanks Marc

Link to comment
Share on other sites

You have to load a PHP file to work with form data, since PHP is on the server, another HTTP request for the PHP script has to be sent. You could put some PHP code in the current page and make the form submit to its own page. If you want to redirect to a new page, look at PHP's header() function.

Link to comment
Share on other sites

I decided to just go to a PHP file called login where I will do the check, if it passes sent Header to the menu page, if it fails use header() to go back to the Index page. Hope this should work. However I am now having fun with my PHP file and accessing Xampp web server. Grrr! That will be in a different post shortly.

Link to comment
Share on other sites

Well this sounds a lot like my login screen. You want it so when they log in it takes them to a different page.Which my login.php I have the form send to itself.If you haven't tried this before this is how it works. In the <form action="..." >The action will be the same exact file this form is in.So say the page this form is on is login.php the action="login.php" The only new thing you might have to add in the form is:<input type="submit" name="submit" value="Login" /> Now that you gave submit a name you can test for it. You are going to add a php tag before any of the html loads. (It can be after some php but just make sure it is before your html.)

<?php if(isset($_POST['submit'])){$username = $_POST['txtUserName'];$password = $_POST['txtPassword'];$query = "SELECT * FROM table WHERE username = '{$username}' AND password = '{$password}' LIMIT 1"; $result_set = mysql_query($query);confirm_query($result_set);if(mysql_num_rows($result_set) == 1){//Both Athenticated//+1 only one match$found_user = mysql_fetch_array($result_set);redirect_to('ssystaffindex.php');} }?>

The two functions confirm_query() and redirect_to():

function redirect_to($location = NULL){  if ($location != NULL) {   header("Location: {$location}");   exit;  }}function confirm_query($result_set) {  if (!$result_set) {   die("Database query failed: " . mysql_error());  }

confirm_query() is just making sure the query worked.redirect_to() will then redirect them to a certain page AFTER everything was finished. So in the () you put your destination like; redirect_to('index.php'); If I didn't explain my code enough let me know.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...