View Full Version : Form Issue with a <button>

11-04-2010, 05:03 AM
I am having a problem with a form on my site. There are two (2) buttons: 1 submit button and 1 <button>. The submit button submits the entire form to a function for error checking. The <button> button is used to pull the information from a field, put it into an array, and then display that array in a field below the <button> button.
I tried putting a form into the form and making the <button> button a second submit button, thinking when that submit button is hit, it would just do the action I told it to do. However, it submitted the whole form. I need the <button> button to pull the information from a field from the form and display it in the same form.
I do no want to use javascript right now because since some people can't/don't have javascript enabled, I want to make it work in PHP first. Is there a way to make what I want to work, work?

11-04-2010, 05:48 AM
What you want to do requires Javascript. There's no way around that. PHP cannot do it alone; once PHP is done parsing the script and generates the HTML for the page, and sends the HTML off to the client's browser, it's done and can't do anything more. At that point, it's all up to the client browser (and thus, Javascript) to do any further manipulation to the HTML, DOM, or form values. The page must be reloaded otherwise.

One way to bypass a reload is to use AJAX, but AJAX is Javascript so that wouldn't fulfill your requirements to not use Javascript.

11-04-2010, 06:47 AM
The thing is, I plan to implement javascript later. But for the folks who don't have javascript enabled, I need to give them an alternative method. I understand that the page will need to refresh for the script to work. I was just wondering if there was a way to use POST with a <button> command. Would it have to be a SESSION variable?

11-04-2010, 06:59 AM
Uh... you don't use POST with a form object... you use it with a form. So sure, you can use POST with a form that uses a button. But I don't think that's what you meant to ask. So you don't mind refreshing? You'll pull the details on the current form, use them to get other information, then want all that to display back on the form?

11-04-2010, 07:21 AM
Correct. The only reason I am doing this in PHP is for the people who don't/can't have javascript enabled. I understand the only way to do this is to have the page refresh. I want a button to pull the information from a text box and display it on the form, beneath the text box from whence it was pulled.
But the issue comes from the fact I already have a submit button for the form, so having two submit buttons won't work. I tried.
What I'm attempting to do is allow the user to input a list of ingredients for a recipe form. I want the ingredients to be displayed below the text box whenever the user hits the 'ADD' button. But the 'ADD' button shouldn't submit the entire form, just what's in the ingredient text box. Once the user has listed all the ingredients for the recipe, then they submit the form via the submit button.

11-04-2010, 08:02 AM
I am also interested in this type of technique.

Keleth, when I was talking about my cascading menus, if I remember correctly you were telling me that submitting the form 3 times was not really viable. The same thing is happening here with this OP.

I can relate to this to the way facebook inserts information into their databases when you post a status update. The window section of page expands and makes room for the new post, data is inserted in the database, but the page does not reload. And as far as I know, PHP is used by facebook for the majority of their scripting.

11-04-2010, 08:15 AM
wojo: two submit buttons do work, as long as they have different names.

What you should do IMO, is give the final submit button a name like final_submit, and the one to pull the details as detail_submit. In your PHP, have an if that tests if 'details_submit' is set, if it is, pull the details, store the form data into a session, and then go back a page, with something like a get variable set or a session variable that holds the state. If that particular variable is set, go ahead and print out the form data along with the newly pulled details. If 'final_submit' is set, instead proceed with the form. Thats the best way I think. The individual button names are of course, up to you.

ajetrumpet, I didn't mention anything about 3 pulls being not viable. If you're avoiding javascript, its how it has to be done, and I see no problem with that.

That being said, don't be afraid of Javascript I say. I've been avoiding JS like crazy on a few sites, and when I actually did an analysis of my visitors, I found a majority of them supported Javascript. So you're dead right to have PHP backup code for those who don't, but don't be afraid of Javascript because of those who can't support it.