4 Replies - 9217 Views - Last Post: 26 October 2011 - 08:17 PM

#1 laytonsdad   User is offline

  • Let it rip!
  • member icon

Reputation: 466
  • View blog
  • Posts: 1,992
  • Joined: 30-April 10

pass javascript variable to php for sql query before submission

Posted 25 October 2011 - 09:52 AM

I am working on a log in page and I want to be able to query using the username that a user puts in a form to select a security question from a MySQL database that was added when the user registered.

I am using PHP to do the log in and I don't know the best way to pass the username after the user enters it to PHP to query the security question so it will be shown in the form before submission so the user can answer it and then submit and it can be tested for a match in there row of data on the database.

The form is

<form action="login.php" method="post">
<table id="login_table" cellpadding="0" cellspacing="5" border="thin silver solid" class="login_table">
	<tr>
		<th class="login_head" colspan="2">Log in</th>
	</tr>
	<tr>
		<th class="labels">User Name:</th>
		<td class="inputs"><input id="userName" name="userName" type="text" onblur="get_userName()"></td>
	</tr>
	<tr>
		<th class="labels">Password:</th>
		<td class="inputs"><input name="password" type="password"></td>
	</tr>
	<tr>
		<th class="labels">Question:</th>
		<td class="inputs">
			<?php			
				if (@mysql_connect('REMOVED', 'REMOVED', 'REMOVED'))
				{
					if (@mysql_select_db('REMOVED'))
					{
						$getQuery = "SELECT question FROM users WHERE userName = 'javascript variable'";
						
						$result = mysql_query($getQuery);
						
						while($row = mysql_fetch_array($result))
						{
							print $row[0];
						}
					}
					else
					{
						print mysql_error();
						print "<input type='button' value='Back' onclick='history.back()'/>";
					}
				}
				else
				{
					print mysql_error();
					print "<input type='button' value='Back' onclick='history.back()'/>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th class="labels">Answer:</th>
		<td class="inputs"><input name="answer" type="text" maxlength="100"></td>
	</tr>

	<tr>
		<td colspan="2" class="center">
		<input name="submit" type="submit" value="Login"></td>
	</tr>
</table>
</form>


Does anyone have a simple solution to this problem, I am not proficient in Javascript or AJAX.
I have done some Google searches but I don't know the proper terminological to use so I am not coming out with much that is useful.

This post has been edited by laytonsdad: 25 October 2011 - 09:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: pass javascript variable to php for sql query before submission

#2 sas1ni69   User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: pass javascript variable to php for sql query before submission

Posted 25 October 2011 - 10:11 PM

You should try something like this;

$username = $_POST['username'];



You can then pass your variable into your query. You can also do some little checking like if it's set like this;

if (!$_POST['username'] | !$_POST['password']) {

    die('Missing fields');

}



Another tip to keep in mind is to try to separate your HTML and your PHP code. It'll serve you really well in the future. It'll keep your code organize and a lot easier to maintain.

And one last thing, I recommend you watch this video. It's a multi part tutorial on user registration and login. Try to see and learn what you can. PHP Academy has helped me tremendously and I still refer to the tutorials all the time. Maybe it'll help you too :)

Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: pass javascript variable to php for sql query before submission

Posted 26 October 2011 - 04:12 AM

if (!$_POST['username'] | !$_POST['password']) {

should be
if (!$_POST['username'] || !$_POST['password']) {


Quote

Does anyone have a simple solution to this problem, I am not proficient in Javascript or AJAX.


So how about trying to learn them? If you're going to write web pages, you're going to need to.

You need to write a Javascript function for the onblur event to call PHP page with SQL query through AJAX. This page will return the data you want in a JSON object, which you then use to populate the html element(s) you want to fill with the data. There are tutorials everywhere on how to do this.
Was This Post Helpful? 0
  • +
  • -

#4 laytonsdad   User is offline

  • Let it rip!
  • member icon

Reputation: 466
  • View blog
  • Posts: 1,992
  • Joined: 30-April 10

Re: pass javascript variable to php for sql query before submission

Posted 26 October 2011 - 12:49 PM

View PostJackOfAllTrades, on 26 October 2011 - 04:12 AM, said:

So how about trying to learn them? If you're going to write web pages, you're going to need to.

You need to write a Javascript function for the onblur event to call PHP page with SQL query through AJAX. This page will return the data you want in a JSON object, which you then use to populate the html element(s) you want to fill with the data. There are tutorials everywhere on how to do this.


Thank you for the post. I did learn the technique and it is great, I used jquery. I still need to understand the ajax xmlhttp object a little more But I think I'm well on the way.
Thank you to all that posted a message.
Was This Post Helpful? 0
  • +
  • -

#5 sas1ni69   User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: pass javascript variable to php for sql query before submission

Posted 26 October 2011 - 08:17 PM

[quote name='JackOfAllTrades' date='26 October 2011 - 07:12 PM' timestamp='1319627537' post='1470339']
if (!$_POST['username'] | !$_POST['password']) {

should be
if (!$_POST['username'] || !$_POST['password']) {


Spot on. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1