Jump to content

[Help] I can't work out how to make a php file to make this <form> work..


Beanmen

Recommended Posts

Hi there, As the question says i tried several things but i can't work it out and my knowledge about php isn't that well.

 

<form>

                   <div class="text-box">

                    <label>

<input type="text" class="textbox" value="Your Name:" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Name:';}">

</label>

<label>

<input type="text" class="textbox" value="Your Email:" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Email:';}">

</label>

<div class="clear"></div>

</div>

<textarea value="Your Message:" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Message:';}">Message</textarea>

<input type="submit" value="Send Message">

</form>

You might be imagining this part of PHP wrong, 

 

PHP is Pre HyperText Processor, means, anything to do with PHP on page, will be processed before displaying HTML to user.

If you were to use PHP inside forms or let say, you wanted the form to be PHPized,

 

There could be two ways, as in my humble opininon,

<?php

	/* suppose we have some variables or data coming from PHP
	and we want to use them in HTML form - I am using hardcoding
	for simplicity and better understanding */
	
	$username = "theUser";
	$group = "admin";
	
	if($group == 'admin'){$isAdmin = true;} else {$isAdmin = false;}
	
	/* One way is as follows, to use the HTML inside PHP
	------------------------------------------------------------------------------
	I want so set a condition that if username is declared and user 
	group is admin, display the following form */
	
	if($isAdmin){
		echo("
			<form name='myForm' method='post' action='" . $_SERVER['PHP_SELF'] . "'>
				<input type='text' name='username' value = '" . $username . "' />
				<input type='submit' name='submitForm' value='Go' />
			</form>
		");
	} else {
		echo "You do not have permission to do blah blah";
	}
	
	/* Note that we have been doing everyhting of this inside <?php  ?> tag
	Now we will try to use the same form outside <?php tag */
?>

	<!--
		The Other way is outside php tag and using html straight
		Please not that how I am using <?php //code ?> tags inside
		the form to get data from PHP
	-->
	
	<?php if($isAdmin){ ?>
	<form name="myForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
		<input type="text" name="username" value="<?php echo $username; ?>" />
		<input type="submit" name="submitForm" value="Go" />
	</form>
	<?php } else { ?>
	<span>You do not have permission to do blah blah</span>
	<?php } ?>

I hope you will get it. :)

@Beanmen : When you post code in the forum, you can use the 'code' button and paste your code in the popup that will appear, this way the code will have colour and will have a better format.

 

That being said, you should tell us:

  • The example you provided only has HTML, no PHP code. Do you have PHP installed on your computer? Or do you plan to code directly on a web server, for example by uploading PHP files with FTP? If you don't have PHP installed, you should check out XAMP (for Mac) or WAMP (for Windows), it will install PHP on your machine for you.
  • Do you want to create a contact form that will be send by email? It looks like this from your field names. If that's what you want, you can check out my tutorial: http://www.mogosselin.com/simple-php-contact-form/  (shamelessplug) or check Google for 'php contact form', I'm sure there is a ton of those. ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.