0

I have been aroundsites a while and I have learnt that ajax isn't that dynamic.
I tried to use iframes but got caught with cross frame AJAX and PHP with parent page.
http://www.daniweb.com/forums/thread262323.html
Which no one answered on, so I went to AJAX.

I call a php form for users to register from an AJAX, but not being dynamic it doesn't pass any of the variables across.

<form id="loginForm" name="loginForm" method="post" action="javascript:ajaxpage('uselog/register-exec.php', 'radio');">

How would I turn it dynamic?
Thanks

Edited by Mouse1989: n/a

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by kishan112
-1

Thanks for the very helpful post.
Did not contribute anything to the discussion.

0

You then have to put the full source here, otherwise we can't help you.
Ju must put the ajax function you're working with

0

I'm trying to get this form working, and to use AJAX include to move across pages and div tags. However as far as I know AJAX is static and the PHP does not execute.

register-form.php

<form id="loginForm" name="loginForm" method="post" action="javascript:ajaxpage('uselog/register-exec.php', 'radio');"> 
<table width="231" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td colspan="2">Already registered? <strong><a href="javascript:ajaxpage('uselog/login-form.php', 'radio');">Login!</a></strong></td>
    </tr>
    <tr>
      <th width="120">First Name</th>
      <td width="103"><input name="fname" type="text" class="textfield" id="fname" size="20"/></td>
    </tr>
    <tr>
      <th>Last Name</th>
      <td><input name="lname" type="text" class="textfield" id="lname" size="20"/></td>
    </tr>
    <tr>
      <th>Login</th>
      <td><input name="login" type="text" class="textfield" id="login" size="20"/></td>
    </tr>
    <tr>
      <th>Password</th>
      <td><input name="password" type="password" class="textfield" id="password" size="20" /></td>
    </tr>
    <tr>
      <th>Confirm</th>
      <td><input name="cpassword" type="password" class="textfield" id="cpassword" size="20" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Register" /></td>
    </tr>
  </table>
</form>

uselog/register-exec.php

<?php
	//Start session
	session_start();
	
	//Include database connection details
	require_once('config.php');
	
	//Array to store validation errors
	$errmsg_arr = array();
	
	//Validation error flag
	$errflag = false;
	
	//Connect to mysql server
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	
	//Select database
	$db = mysql_select_db(DB_DATABASE);
	if(!$db) {
		die("Unable to select database");
	}
	
	//Function to sanitize values received from the form. Prevents SQL injection
	function clean($str) {
		$str = @trim($str);
		if(get_magic_quotes_gpc()) {
			$str = stripslashes($str);
		}
		return mysql_real_escape_string($str);
	}
	
	//Sanitize the POST values
	$fname = clean($_POST['fname']);
	$lname = clean($_POST['lname']);
	$login = clean($_POST['login']);
	$password = clean($_POST['password']);
	$cpassword = clean($_POST['cpassword']);
	
	//Input Validations
	if($fname == '') {
		$errmsg_arr[] = 'First name missing';
		$errflag = true;
	}
	if($lname == '') {
		$errmsg_arr[] = 'Last name missing';
		$errflag = true;
	}
	if($login == '') {
		$errmsg_arr[] = 'Login name missing';
		$errflag = true;
	}
	if($password == '') {
		$errmsg_arr[] = 'Password missing';
		$errflag = true;
	}
	if($cpassword == '') {
		$errmsg_arr[] = 'Confirm password missing';
		$errflag = true;
	}
	if( strcmp($password, $cpassword) != 0 ) {
		$errmsg_arr[] = 'Passwords do not match';
		$errflag = true;
	}
	
	//Check for duplicate login ID
	if($login != '') {
		$qry = "SELECT * FROM members WHERE login='$login'";
		$result = mysql_query($qry);
		if($result) {
			if(mysql_num_rows($result) > 0) {
				$errmsg_arr[] = 'Login name already in use';
				$errflag = true;
			}
			@mysql_free_result($result);
		}
		else {
			die("Query failed");
		}
	}
	
	//If there are input validations, redirect back to the registration form
	if($errflag) {
		$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
		session_write_close();
		header("location: register-form.php");
		exit();
	}

	//Create INSERT query
	$qry = "INSERT INTO members(firstname, lastname, login, passwd) VALUES('$fname','$lname','$login','".md5($_POST['password'])."')";
	$result = @mysql_query($qry);
	
	//Check whether the query was successful or not
	if($result) {
		header("location: register-success.php");
		exit();
	}else {
		die("Query failed");
	}
?>

However when I"submit" to register, it returns that all the fields are blank because nothing is picked up.

I tried using iframes before, but could not include the AJAX of the _parent page into the PHP.

//Check whether the query was successful or not
	if($result) {
		header("location: register-success.php");
		exit();
header("location: register-success.php");

=
I tried something along the lines of

header("location:javascript:ajaxpage('uselog/register-success.php', 'regBox');" target="_parent"");

_parent

<script type="text/javascript">

						ajaxpage('home.html', 'regBox')

				</script>

Edited by Mouse1989: n/a

0

I'm sorry to say it again, but you don't understand the way AJAX works. That's why I told you to take a little tutorial to understand the basics.

I'll give you an example how you can dynamically check if the loginID is duplicate using AJAX.
Please correct the php page with the database connections etc. Make sure you see "false" when running the php and there is a duplicate loginID
This is uselog/logincheck.php:

<?php


$login = clean($_GET['login']);

//Check for duplicate login ID
	if($login != '') {
		$qry = "SELECT * FROM members WHERE login='$login'";
		$result = mysql_query($qry);
		if($result) {
			if(mysql_num_rows($result) > 0) {
				echo "false";
			}
			@mysql_free_result($result);
		}
		else {
			die("Query failed");
		}
	}


?>

If the php is running correct, then test the following form.
If you input a duplicate loginID, you will see a warning message dynamically :)

<html>
<head>
<title>Register</title>
<script>
function getHTTPObject(){
   if (window.ActiveXObject)
    return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest)
    return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX.");
      return null;
   }
}
function checkLogin(login){
	lcheckObject = getHTTPObject();
	lcheckObject.open("GET", "uselog/logincheck.php?login="+login+"&rand="+Math.random()*999, true);
	lcheckObject.onreadystatechange = loginCheckResults;
	lcheckObject.send(null);
}
function loginCheckResults(){
	if(lcheckObject.readyState == 4){
		if (lcheckObject.responseText == 'false'){
			alert("LOGIN ALREADY EXISTS");
		}
	}
}
</script>
</head>
</body>
<form id="loginForm" name="loginForm" method="post" action="uselog/register-exec.php"> 
<table width="231" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td colspan="2">Already registered? <strong><a href="javascript<b></b>:ajaxpage('uselog/login-form.php', 'radio');">Login!</a></strong></td>
    </tr>
    <tr>
      <th width="120">First Name</th>
      <td width="103"><input name="fname" type="text" class="textfield" id="fname" size="20"/></td>
    </tr>
    <tr>
      <th>Last Name</th>
      <td><input name="lname" type="text" class="textfield" id="lname" size="20"/></td>
    </tr>
    <tr>
      <th>Login</th>
      <td><input name="login" type="text" class="textfield" id="login" size="20" onchange="checkLogin(this.value);"/></td>
    </tr>
    <tr>
      <th>Password</th>
      <td><input name="password" type="password" class="textfield" id="password" size="20" /></td>
    </tr>
    <tr>
      <th>Confirm</th>
      <td><input name="cpassword" type="password" class="textfield" id="cpassword" size="20" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Register" /></td>
    </tr>
  </table>
</form>
</body>
</html>

The javascript is the AJAX part (XMLHttpRequest)..
I hope you'll understand this and see how AJAX is actually working

Edited by kishan112: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.