So...I'm not exactly a genius when it comes to PHP...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ScottRiley
Forum Newbie
Posts: 6
Joined: Tue Aug 08, 2006 7:44 am

So...I'm not exactly a genius when it comes to PHP...

Post by ScottRiley »

Hi, I've just signed up because I have a pretty annoying problem. I have created a register form and coded it correctly, linking it to a 'register.php' page, all my code works, and the information can be added to the server, but I'm at a loss as to how to prompt the user on the same page as they enter the form.

Basically, with the way I have coded it, the errors/output appear on a new page. I know that this is obviously because my 'register.php' file is a separate file from the HTML file containing the form, but I just can't work out how to get the errors to appear where I want them, do I need to echo HTML to create the form, or is it just a matter of where my PHP code is located on the HTML file.

Sorry if this question has been asked before, but I woud appreciate some help,
Thanks.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Probably best to show us your code.

Not forgetting to read the posting guidelines first, especially how to correctly post code
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Visitor visits page.

Completes form and hits submit.

The form is submitted to it's self if there is no errors it redirects to another page.

The logic shows up something like this:

Code: Select all

if( $_POST['submit'] == 'Submit' ){
//Do some error checking
  if(!$error){
     //Process the data.
     //Redirect to success page.
    header("location: http://someaddress");
  } else {
      //Show errors and load form
      //<input type= "text" name="somefield" value="$_POST['somefield']" /> 
   }
} else {
//Show html form
    
}
ScottRiley
Forum Newbie
Posts: 6
Joined: Tue Aug 08, 2006 7:44 am

Post by ScottRiley »

The code for my PHP file is:

Code: Select all

<?php

//START PHP CODING HERE!!!!!!!!!!!!!!!!!!!!!!!
session_start();
include("connect.php");
//first, check if the user has submitted information
if(isset($_POST['busname']))
{
//get the information from the form
$businessname = $_POST['busname'];
$username=$_POST['busname'];
$password1=$_POST['password1'];
$password2=$_POST['password2'];
$email=$_POST['email'];
$postcode=$_POST['PostCode'];
$contactname=$_POST['cont_name'];
$ContactNumber = $_POST['contactnumber'];
$HouseName = $_POST['HouseName'];
$Road = $_POST['Road'];
$Town = $Post['Town'];
$County = $_POST['County'];

	//now check passwords match
	if($password1 != $password2)
	{
		die('Please ensure passwords match');
	}
	//check if passwords meet a minimum length
	if(strlen($password1) < 6)
	{
		die('Password must be 6 characters or longer');
	}
	//check if anything is null
	
	if($_POST['busname'] = "")
	{
		die('Please fill in business name');
	}
	if($_POST['password1'] = "")
	{
		die('Please fill in password');
	}
	if($_POST['password2'] = "")
	{
		die('Please fill in password');
	}
	if($_POST['email'] = "")
	{
		die('Please fill in email');
	}
	if($_POST['PostCode'] = "")
	{
		die('Please fill in postcode');
	}
	if($_POST['cont_name'] = "")
	{
		die('Please fill in contact name');
	}
	if($_POST['contactnumber'] = "")
	{
		die('Please fill in contact number');
	}
	if($_POST['HouseName'] = "")
	{
		die('Please fill in House Number');
	}
	if($_POST['Road'] = "")
	{
		die('Please fill in road');
	}
	if($_POST['town'] = "")
	{
		die('Please fill in town');
	}
	//check if user/email exists
	$userquery = mysql_query("SELECT username FROM southport_businesses WHERE username ='$busname'");
	$useravailable = mysql_num_rows($userquery);
	$emailquery= mysql_query("SELECT email FROM southport_businesses WHERE email ='$email'");
	$emailavailable = mysql_num_rows($emailquery);
	if($useravailable > 0)
	{
		die('Username is taken, please select another');
	}
	if($emailavailable > 0)
	{
		die('E-mail has already been registered');
	}

	//ADD DATA TO TABLE
	mysql_select_db('gblocal1');
	if(mysql_query("INSERT INTO southport_businesses(id, BusinessName, Username, ContactNumber, email, pword, PostCode, HouseName, Road, Town, County, ContactName) VALUES(0,'$businessname','$username','$contactnumber','$email','$password1','$postcode','$HouseName','$Road','$Town','$County','$ContactName')"))
	{
		echo('Creation Succesful');
	}
	else
	{
	  echo('Creation Failed');
	}
	//SEND VALITATION EMAIL
	//REDIRECT TO THANK YOU PAGE
}
else
{
  echo("Meh");
}
?>
And if all goes well, i get a blank page with:

Code: Select all

Creation Succesful
Should I replace that creation succesful with a link to another page? That's the idea I got from reading your response...

neophyte, the

Code: Select all

else { 
//Show html form 
    
}
in your post is what I'm struggling with, I just don't know how to show the html form through PHP, should I just do it with an 'echo' statement, or is there a better way?

Sorry if this is annoying btw, I just really need to fix it...
Post Reply