Page 1 of 1

Beginner Needs Help

Posted: Wed Dec 01, 2010 2:22 am
by dilby
Hi all - I'm a designer trying to cut his teeth on some php. I have a real simple task to do and was hoping for some help.

All I want to try doing first is have a page that says

'Hi my name is.....'

And have the blank updatable via a page that has a text field and a save button. I already have made a database, but not sure what to
create in the way of tables etc.

I thought it'd be easy to find a tutorial for this but no luck, so any help would be immensely appreciated!

Thanks!

Re: Beginner Needs Help

Posted: Wed Dec 01, 2010 3:27 am
by social_experiment
dilby wrote:And have the blank updatable via a page that has a text field and a save button. I already have made a database, but not sure what to
create in the way of tables etc.
The form code and 'display' code

Code: Select all

 <!-- the html form -->
<form method="post" action="page_to_display" >
<input type="text" name="text_field" size="20" maxlength="50" />
<input type="submit" name="save" value="Save" />
</form>

Code: Select all

<?php
 // u need an html form, using post as a method
 // on the previous page
 
// ---
 // check that the 'save' button is clicked
 // or you will have 'Hi my name is.....'  shown
 // without a name
 if (isset($_POST['save'])) {
 // remove whitespace if any
  $name = trim($_POST['text_field']);
 // stop possible XSS attacks
  $clean_name = htmlentities($name);

 // print to browser
  echo 'Hi my name is '. $clean_name;
 }
?>
Your table should have an id field that is auto incrementing and a name field that is set to varchar with a length of 100 (or 255 if you want) for the names people enter.

Re: Beginner Needs Help

Posted: Wed Dec 01, 2010 4:57 pm
by dilby
Hi thanks fo much for the reply! Please excuse my green questions, but what do I need to name the database table? And does this code go into the one file - if so what is the code to actually display it on say an index page in the root?

Thanks!

Re: Beginner Needs Help

Posted: Wed Dec 01, 2010 5:19 pm
by Pazuzu156
Hey, I make a simple coded one to show what you are asking and doesn't require a database.

Code: Select all

<?php

if(isset($_POST['submit'])){
	$name = $_POST['name'];
	
	if($name){
		echo "Hi, my name is $name";
	} else {
		echo "Plese enter your name.";
	}
} else {
	echo "
		<form method='POST' action='name.php'>
			Type your name in the textfield bellow and press the button.<br />
			<input type='text' name='name' size='30' /><br />
			<input type='submit' name='submit' value='Submit' />
		</form>";
}

?>
Maybe you will find this useful.

Re: Beginner Needs Help

Posted: Wed Dec 01, 2010 8:20 pm
by califdon
Here's a fairly decent tutorial that covers what you are asking about:
http://www.blazonry.com/scripting/links ... t_data.php