Jump to content
Larry Ullman's Book Forums

2 Forms On One Page


Recommended Posts

Hello all,

 

I need to include 2 forms on one page. One will be a login form and the other will be a form for the logged in user to change his profile. The site is a modular design, so all links (including form submissions) will call the same page.

 

The problem is this line of code which handles whether a user has submitted the login form:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
include ('../../includes/gallery_login.inc.php');
...
}

This is fine if the user is logging in, but what if they are submitting the change profile form? Does anyone know the best way of handling this? Is it possible to check the POST data to determine which form was submitted?

 

Thanks,

 

Matt

Link to comment
Share on other sites

Thanks for the link, Matt. But isn't there a problem sometimes with the "submit" input field? On Internet Explorer, maybe? I thought that was the reason why Larry often uses the hidden "submitted" field to check for submission.

 

Edit: here it is, from Larry's "PHP 6 and MySQL 5" QuickPro guide:

 

Another common method for checking if a form has been submitted is to see if the submit button’s variable—$_POST[‘submit’] here—is set. The only downside to this method is that it won’t work in some browsers if the user submits the form by pressing Return or Enter.

 

If you use an image for your submit button, you’ll also want to use a hidden input to test for the form's submission.

Link to comment
Share on other sites

Thanks for the link, Matt. But isn't there a problem sometimes with the "submit" input field? On Internet Explorer, maybe? I thought that was the reason why Larry often uses the hidden "submitted" field to check for submission.

 

Yeah, I agree, and was torn between both solutions. After doing some more research I found out that the "hidden" input method used to be the standard way to detect multiple form submissions on a page. However, the new way seems to be to use the "submit" input field. It is much cleaner and apparently all new browsers support it.

 

According to W3Schools browser statistics, IE only has 22% of the browser market now. IE is loosing it's position big time, and it's about time. Due to these pressures, and the growing usage of competitor's browsers, I think Microsoft is finally getting their act together and creating standards compliant browsers. As far as IE6 and before, my only response is to upgrade to a proper browser! I think I speak for the world when I say that I am tired of playing the compatibility game with IE :angry:

 

Anyway, things have certainly changed since "PHP 6 and MySQL 5" QuickPro Guide came out, so this would be a good time to see what Larry has to say on the topic. May be we can even take a poll in the forum: "hidden" vs. "submit"

Link to comment
Share on other sites

I would just use a hidden input to indicate the form that was submitted. It's 100% reliable and easy to implement. As for IE usage, the last numbers I saw were around 45%. In either case, you can't knowingly make something that won't work for one-quarter or one-half of your potential visitors.

Link to comment
Share on other sites

Don't display the login form is the user is logged in. If the are not logged in, they don't need the "update profile" form. :)

 

Thanks Antonio! No, I don't display the login form once the user is logged in. However, I am using the modular site design that is in Chapter 3, so everything is being processed within one page. Anytime the page is reloaded using the POST method it has a listener within conditionals to see which hidden input was sent.

 

Also, a guest can leave a message on the page and this will require a second form as well.

Link to comment
Share on other sites

I had another question. I was looking at the way other people have implemented checking for hidden inputs and I found that there are two ways to do it.

 

Here is an example form:

<form name="contactus" method="post">
<input type="text" name="email" />
<input type="text" name="subjet" />
<textarea name="message"></textarea>
<input type="hidden" name="contact" value="1"/>
<input type="submit" name="contact-submit" value="Send Email" />
</form>

Some just test if the hidden input is empty in the $_POST array:

if(!empty($_POST['contact']))
{...

And others use the "array_key_exists" method:

if(array_key_exists('contact', $_POST))
{...

Which way is better? Are they both the same?

 

Thanks,

 

Matt

Link to comment
Share on other sites

if(!empty($_POST['contact']))
{...

And others use the "array_key_exists" method:

if(array_key_exists('contact', $_POST))
{...

Which way is better? Are they both the same?

 

No, they are not the same. There's an important difference.

 

$_POST Description from php.net:

An associative array of variables passed to the current script via the HTTP POST method.

 

By using array_key_exists('contact', $_POST), you check wheter the key 'contact' exsists inside the POST array. You can't know (by this function) if it's empty, only that it's there.

 

This is why it's so important to read the manual. It's really a great tool. Use it. :)

 

I'm getting tired here. Hope this makes sense.

  • Upvote 1
Link to comment
Share on other sites

Jeg er norsk, ja. Er du norsk?

 

My real name is Thomas, btw. Antonio Conte is my first favorite player - and the current coach - at the Italian Football Club Juventus.

 

You are almost right about your assumption. The function empty() does however not know if the array key exists, but it would have to be set, not to be empty. :)

Link to comment
Share on other sites

Jeg er norsk, ja. Er du norsk?

 

My real name is Thomas, btw. Antonio Conte is my first favorite player - and the current coach - at the Italian Football Club Juventus.

 

Jeg er 1 / 4 norsk, og jeg bodde i Stavanger i 6 måneder. Jeg boddesammen med venner. (I can read and write basic Norwegian, but I still use Google translate to make sure it's correct ;) ) Yes, the name Antonio can be a bit misleading. Hartleysan told me you were Norwegian!

 

One more question: Would it be better to use isset() in this case? I think that's what Larry uses in PHP6 and MySQL5 for Dynamic Websites: Visual Quickpro Guide.

 

Matt

Link to comment
Share on other sites

Stilig! Visste ikke at du var litt norsk av deg. Stavanger er en fin by. Bodde der i et år når jeg var rundt 3 år gammel.

 

Isset does pretty much the same as array_key_exists(). It just check wheter the variable are set or not. Isset() is great to use when you don't care about what the variable contains, but just that it exists. A great use for isset() would be to check wheter $_POST is set or not.

 

But Matt: I really just recommend you to check these function on php.net. You'll learn alot about functions you didn't know you needed to know. A good example are integers and forms. All form input sent with $_POST are sent as Strings(!). This mean you cannot use functions like is_int() without typecasting, but the function ctype_digit() works the same way with strings. A good test is therefor to use something like this:

 

if ( is_int($var) || ctype_digit($var) ) {


  // we are sure this is an integer or a string representation of an integer. Bulletproof
}

 

Things like these are not obvious, but that's why the php manual is indespensable. Be sure to check "related functions" and their description beneth functions. That's how you stumple upon things like these. .)

 

But really. I don't mind explaining, but I guess it's faster for you.

Link to comment
Share on other sites

Apart from the PHP manual, what I find very useful is creating test files using the different functions or conditionals I'm trying to better understand. For instance, I created a test file with a simple form, and conditionals printing the results when using isset(), empty(), if ($var), is_numeric(), and so on. This way, when I wonder which function/conditional I should use in a script, or why a script is not doing what I want it to do, I just have to use the test file to remind me of the differences between isset(), empty() and so on.

  • Upvote 1
Link to comment
Share on other sites

Thank you all for the great input!

 

@Josee - Yeah, I did something similar a while back to see what the difference between !isset() and empty() was ;)

 

I have never heard of ctype before. Thanks for that!

 

Matt

Link to comment
Share on other sites

 Share

×
×
  • Create New...