Beginner Needs Help

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
dilby
Forum Newbie
Posts: 6
Joined: Tue Nov 09, 2010 11:08 am

Beginner Needs Help

Post 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!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Beginner Needs Help

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
dilby
Forum Newbie
Posts: 6
Joined: Tue Nov 09, 2010 11:08 am

Re: Beginner Needs Help

Post 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!
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Beginner Needs Help

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Beginner Needs Help

Post by califdon »

Here's a fairly decent tutorial that covers what you are asking about:
http://www.blazonry.com/scripting/links ... t_data.php
Post Reply