Form on same page as validator!

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
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Form on same page as validator!

Post by wmasterj »

My case:
I got a form

Code: Select all

<?php
echo "<hr width='520' align='left'><form action="admin.php?page=siteEditor.php" method="post"><font face='verdana' size='2'><b>Create new section:</b></font>";
echo "<br><font face='verdana' size='1'>Section name: <input type="text" name="sectionname" class="editor"><br><br><input type="submit" name="submitsection" value="Create New Section" class="editor"><br>";
echo "</font><br><br>";
?>
and then i got the piece of code which, is on the same page and which takes care of the data inputted.

Code: Select all

<?php
//Adding a new section
if(isset($submitsection))
{
	echo "Yes I got in!";
	sectionAdd($sectionname);
		//Resetting variables so you can make a new .one withouT
		//refreshing the page
		$submitsection = "";
		$sectionname = "";
}
?>
The thing is nothing happens, and iv'e spent a very long time trying to figure it out... :? Not even tha text Yes I got in! shows, soo....
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+.
It's not a sticky thread for nothing and might solve your problem ;)
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Post by wmasterj »

Things is i got -> register_globals on
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Dont take me wrong, but are you sure?
I happen to have it off, but after changing the code to

Code: Select all

if(isset($_POST['submitsection']))
...it worked here just as you pasted it.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: Form on same page as validator!

Post by McGruff »

Does it work like this, with the form action set to $_SERVER['PHP_SELF']?

Code: Select all

<?php
echo '<hr width="520" align="left" /><form action="' . $_SERVER[PHP_SELF] . '" method="post"><font face="verdana" size="2"><b>Create new section:</b></font>';
echo '<br><font face="verdana" size="1">Section name: <input type="text" name="sectionname" class="editor"><br /><br /><input type="submit" name="submitsection" value="Create New Section" class="editor"><br />';
echo '</font><br /><br />';
?>
Side note: if you use single quotes to declare html strings it saves a lot of escaping. Jump out of the string & concatenate to add a var.

Have also added a space and / to empty tags like <br /> which is the normal, xhtml compliant, way to write them nowadays.

I personally don't llike to have any html in my php scripts but that's another issue.
Last edited by McGruff on Wed Aug 10, 2005 8:12 pm, edited 1 time in total.
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Post by wmasterj »

I got it!
The things was that the rate of success was very inconsistent and so instead of testing $submit section i tested $sectionname:

Code: Select all

<?php
if(isset($_POST['sectionname']) && ($_POST['sectionname'] != ""))
{
	sectionAdd($_POST['sectionname']);
		//Resetting variables so you can make a new one without
		//refreshing the page
		$_POST['sectionname'] = "";
}
?>
so i changed the stuff to use $_POST -> but that wasn't the problem still...very strange, cuz' sometimes it worked and sometimes it didn't...but then i used the $sectionname variable instead and it's works like a charm

Thx guys for the input :D
User avatar
wmasterj
Forum Commoner
Posts: 40
Joined: Mon Aug 18, 2003 5:52 pm
Location: Stockholm, Sweden

Post by wmasterj »

thx McGruff i'll start using tha < /> thing
And thanks for going trough and changing the text and stuff... :)

Why i use double-qoutes is because i can use variables without having to '.$var.' them instead i can just put it rigth in there and PHPedit higlights it nicely so i can se it..

Thx again! :D
Post Reply