First off, I'm sorry if this is the wrong section, I've been visiting this site off and on for awhile now, but today is my first day joining.

Second, I was wondering if there is a way to... auto load, I guess you could say, data from a mssql database, after the first text is entered. I.E. I enter the username, and it loads a question they made earlier from the database.


I can post the code if needed. I'm basically trying to make a Password Change PHP script.

Recommended Answers

All 2 Replies

Post some of the code you've tried.

If what you want to accomplish is load only part of the page after it has been loaded already by the browser, this can only be done with AJAX as far as I am aware.

I can't find my password change one at the moment, but here is my registration one. It's set up basically the same way, just that the question isn't an option, and is supposed to load from the database.

<?php
require_once('recaptchalib.config.php');
require_once('recaptchalib.php');
require_once('db.config.php');

$user_ip = $_SERVER['REMOTE_ADDR'];
$username = isset($_POST['username']) ? mssql_escape_string(trim($_POST['username'])) : '';
$password = isset($_POST['password']) ? mssql_escape_string(trim($_POST['password'])) : '';
$password2 = isset($_POST['password2']) ? mssql_escape_string(trim($_POST['password2'])) : '';
$Question = isset($_POST['question']) ? mssql_escape_string(trim($_POST['question'])) : '';
$Answer = isset($_POST['answer']) ? mssql_escape_string(trim($_POST['answer'])) : '';
$Email = isset($_POST['email']) ? mssql_escape_string(trim($_POST['email'])) : '';
$errors = array();
$success = false;
$success1 = false;
if (isset($_POST['submit'])){
	require_once('db.php');
	
	// Validate user name.
	$result = @mssql_query("SELECT UserID FROM PS_UserData.dbo.Users_Master WHERE UserID = '{$username}'") or die('Failed to verify is the provided user named already exists.');
	if(empty($username)){
		$errors['username'] = 'Please provide a user name.';
	}else if(strlen($username) < 3 || strlen($username) > 16){
		$errors['username'] = 'User name must be between 3 and 16 characters in length.';
	}else if(ctype_alnum($username) === false){
		$errors['username'] = 'User name must consist of numbers and letters only.';
	}else if(mssql_num_rows($result)){
		$errors['username'] = 'User name already exists, please choose a different user name.';
	}
	// Validate user password.
	if(empty($password)){
		$errors['password'] = 'Please provide a password.';
	}else if(strlen($password) < 3 || strlen($password) > 16){
		$errors['password'] = 'Password must be between 3 and 16 characters in length.';
	}else if($password != $password2){
		$errors['password2'] = 'Passwords do not match.';
	}
	// Validate Email and Answer
	if (empty($Answer)){
		$errors[] = 'Please provide an answer.';
	}
	if(empty($Email)){
		$errors[] = 'Please provide your email.';
	}
	// Validate reCAPTCHA.  This is to prevent someone botting account creation.
	$response = recaptcha_check_answer($recaptcha_private_key,$_SERVER['REMOTE_ADDR'],$_POST['recaptcha_challenge_field'],$_POST['recaptcha_response_field']);
	if(!$response->is_valid){
		if($response->error == 'incorrect-captcha-sol'){
			$errors['recaptcha'] = 'Incorrect answer to reCAPTCHA';
		}else{
			$errors['recaptcha'] = $response->error;
		}
	}
	// Persist the new account to the database if no previous errors occured.
	if(count($errors) == 0){
		$sql = "INSERT INTO PS_UserData.dbo.Users_Master
				(UserID,Pw,JoinDate,Admin,AdminLevel,UseQueue,Status,Leave,LeaveDate,UserType,Point,EnPassword,UserIp)
				VALUES ('{$username}','{$password}',GETDATE(),0,0,0,0,0,GETDATE(),'N',0,'','{$user_ip}')";
		$usde = @mssql_query("EXEC PS_UserData.dbo.usp_UsersDetail '$username','$Question','$Answer','$Email'");
		// Remove the @ symbol here to see what the SQL error message is when running the above query in $sql.
		if($result = @mssql_query($sql)){
			$success = "Account {$username} successfully created!";
		}else{
			$errors[] = 'Failed to create a new account, please try again later';
		}
		$result1 = @mssql_result($usde, 0, 'Return Value');
		if(($result1) == 1){
			$success1 = "Account information also added. Please note you won't be able to change it later.";
		}else{
			$errors[] = 'Failed to add account information! Please contact Tyler';
		}
	}
}else
	{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
	<title>JQuery Form Example</title> 
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
	<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
	<script type="text/javascript">var RecaptchaOptions = {theme:'clean'};</script>
	<script type="text/javascript">
	$(document).ready(function(){
		$("#myform").validate({
			debug: false,
			rules: {
				username: "required",
				email: {
					required: true,
					email: true
				},
				password: "required",
				password2: "required",
				answer: "required"
				
			},
			messages: {
				username: "Please let us know who you are.",
				email: "A valid email will help us get in touch with you.",
				password: "Please provide a password.",
				password2: "Please retype your password.",
				answer: "Please provide an answer."
			},
			submitHandler: function(form) {
				// do other stuff for a valid form
				$.post('try.php', $("#myform").serialize(), function(data) {
					$('#results').html(data);
				});
			}
		});
	});
	</script>
	<style>
	label.error { width: 250px; display: inline; color: red;}
	</style>
</head>
<body>

		<form name="myform" id="myform" action="try.php" method="post">
			<div style="width:436px; border:1px solid #000000; padding:16px;">
				User Name
				<input type="text" name="username" style="width:100%;" />
				<div style="height: 5px;">&nbsp;</div>
				Password							
				<input name="password" type="password" style="width:100%;" />
				<div style="height: 5px;">&nbsp;</div>
				Confirm Password							
				<input name="password2" type="password" style="width:100%;" />
				<div style="height: 5px;">&nbsp;</div>
				Select a password question<br />
				<select name="question">
				<option value="What is your favorite movie">What's your favorite movie?</option>
				<option value="What is your favorite animal">What's your favorite animal?</option>
				<option value="Who was your childhood love">Who was your first childhood love?</option>
				<option value="What was your first teachers name">What was your first teacher's name?</option>
				<option value="What is your favorite food">What's your favorite food?</option>
				</select><br />
				What's the answer?!
				<input name="answer" type="text" style="width:100%;" />
				<div style="height: 5px;">&nbsp;</div>
				Set your Email							
				<input name="email" style="width:100%;" />
				<div style="height: 5px;">&nbsp;</div>
				Please type this in the text box below to prove you are human
				<?php echo recaptcha_get_html($recaptcha_public_key); ?>
				<div style="height: 5px;">&nbsp;</div>
				<input type="submit" value="Create Account" />
			</div></form>
<!-- We will output the results from process.php here -->
<div id="results"><div>
</body>
</html>
	<?php
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.