Closed Thread Icon

Preserved Topic: Separating button 'value' from it's label in PHP (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=20882" title="Pages that link to Preserved Topic: Separating button &amp;#039;value&amp;#039; from it&amp;#039;s label in PHP (Page 1 of 1)" rel="nofollow" >Preserved Topic: Separating button &#039;value&#039; from it&#039;s label in PHP <span class="small">(Page 1 of 1)</span>\

 
jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-10-2001 23:23

Can I have a button attach a value other than the 'value' attribute to the $'name' variable when I submit a form via a button?

I want all the submit buttons on a page to say the same thing. My previous solution was to generate unique name attributes, then I ran a loop using variable variables to check which one of those variable was passed. VERY ugly and inefficient code. As a last resort I could lose the buttons altogether, and just use a link with the variable information embedded in the URL, but I'd rather not clog up the URL needlessly.

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-11-2001 04:42

Okay, so since no one responded I went ahead and tried to go with plan B. Only problem is that I can not seem to pass a variable in the URL AND submit the form via javascript simultaneously.

Basically I tried <a href="this.php?itemNum=$dynamicNum" onClick="document.myform.submit();">, but when I do that, the rest of the form is not submitted. How do I work around this? GAH!

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-11-2001 06:10

Why not simple include the variable as a hidden element.

I am not 100% sure but I believe that by passing a string and then passing the form you are creating sum redundancies. Submitting would call the form submit which would pass the values in the URI as would passing a variable in the ?variable form.

Another problem would be that you would need to echo the dynamic number, placing it outside of a php command does nothing.

either
<?
$dunamicNum = 9;
$link= "<a href=\"this.php?itemNum=$dynamicNum\">";
?>

<? echo "$link"; ?>

or
<?
$dunamicNum = 9;
?>

<? echo "<a href=\"this.php?itemNum=$dynamicNum\">"; ?>

Some other stubling block may be that you need to define the name attribute in each input type so that the data sent has some pointer when the php parser goes through the file. That may be the problem.

<input type="text" name="t1" value="home">

it gets passed in the URI and the php file would have $t1 set to the value of the text field, which if unchanged would be home.

There are ways to do everything you just have to be careful when looking for them.

I think that the easiest way would be to pass the variable in a hidden input field.

<input type="hidden" name="dynamicNum" value="<? echo "$dynamicNum"; ?>">

When the form is submitted then variable is passed. Would make for cleaner code, keep all the info in one area, as well as making the submit function lack the problem causing redundancy we all do not want to worry about.

-mage-

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 04-11-2001 17:00

WarMage:: "I am not 100% sure but I believe that by passing a string and then passing the form you are creating sum redundancies. Submitting would call the form submit which would pass the values in the URI as would passing a variable in the ?variable form."

When you pass the "this.php?itemNum=$dynamicNum", it's an HTTP GET, and your form (probably a POST) isn't submitted at all.

Having more than one submit button is probably semantically wrong, unless there are multimple <FORM>s on your page.

Can you better describe the semantics you're wanting?

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-12-2001 20:58

Okay, I guess I made my post too short to explain properly. I already considered everything you mention Warmage. Here is the problem:

I have a list of items in a database. When the page loads, it dynamically lists all the items specifying field names and filling in the values. It also generates a submit button for each item.

I had it working fine, but the way that I did it left much to be desired. Because I wanted all the submit buttons to display "change" on the screen, and since what is displayed is also the value that is passed, I could not pass the dynamic number as a value of the submit button, I worked around it by dynamically generating the NAME. In processing this required me to a run a loop which builds a dynamic variable name, and check each one if it contains "change". This is a ridiculous waste of resources, when I should be able to pass just a number and not even have to run a loop, much less build dynamic variable names.

Using a hidden value won't work, because the only indication of what value needs to be passed comes from which button is clicked to submit the form. PHP doesn't know which button you are going to click before it even builds the page. Maybe javascript can do some tricky solution, but it seems to me there should be a way to make a link submit a form while passing a variable or a submit button pass a variable other than it's 'value' (which can't be just some cryptic number, because it's what shows on the screen).

You might ask why I don't just put each row in a separate form and pass the value in the action URL of each form, but there is also a button at the top to 'change all' which necessitates that all fields get passed.

Then you might ask why I don't just stick with only the change all button. Well, it takes an inordinate amount of processing power to check equivalency on every field in a database, so it's best to have the quick editing option.


Okay, so does that sufficiently explain why I need to figure out some way to pass a variable from a submit button (or a link that submits mind u) other than the value of the submit button? And why I can't use a hidden variable?

[This message has been edited by jiblet (edited 04-12-2001).]

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 04-13-2001 04:06

Sounds more like Submit is semantically the wrong thing here. Since the page is one form, what about checkboxes for the options, and a traditional submit button with the semantics "submit my selections."

OTOH, looping per se won't waste resources that bad unless you're eval()ing inside the loop. How about an array-like identifier for the submit buttons (if thats the semantics you insist on): name="submit[1]" and so forth. That should not require an eval().

I read somebody said that's not legal HTML anymore (brackets in the name attribute), but I believe it will work.

Alternatively, how about using JavaScript to push the values of the individual buttons onto some string, then calling submit() for the form object in JavaScript?

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-13-2001 23:10

I'm designing a practical system, not an exercise in semantics. Saying there should only be one submit button is kind of like saying there should only be one link on a page, you could work around a contraint like that, but at the expense of UI flexibility. I am designing this system for people based on a lot of complaints that they had about entering data into the University Wide Events database. Having to click twice instead of once is not a big deal until you have to edit 500 events, then you start asking why.

As far as looping goes, you are right that it probably doesn't make much difference practically, but consider this. Running an O(n) loop where each step involves creating a variable name, then checking the contents of that variable name to see if it was passed, just to get the loop counter which is a number that could simply be passed, we're talking about at least a (300% * number of entries) increase in processing time to determine the magic number. That is to say that passing an integer surely takes less than 1/3rd the time it takes to initiate a loop counter, check to see if the loop counter is less than the number of rows in the result, build a string out of the loop counter, then check the contents of the variable named after the string. I'm sure you can see how that wouldn't scale well.

Your last comment hits the nail on the head though. I figured this out late yesterday after realizing that unless PHP offered some way to pass additional variables through a tag, only a dynamic change to the page would do what I needed. And Slime just happened to post what I needed in the dHTML section. I simply used a button of null type (i.e., not a submit button) and gave it the attribute:

onClick="document.forms[0].elements[0].value=$eventId;document.forms[0].submit();"

element 0 being a hidden field. Worked like a charm, thanks for the help all 3 of you.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 04-14-2001 07:52

Just a small observation - What will happen if some people have JS turned off? Your form won't be submitted...

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-14-2001 10:17

Well that's exactly why I didn't want to have to use JS. It's not a big deal though, because only 1 person will ever have access to this form at one time, and even requiring a specific browser is not out of the question for this.

It seems to me that PHP should have some way of addressing this issue practically, but it's kind of a deep question, so it may be more appropriate for the PHP mailing list. Otherwise I should at least submit it to the PHP developers.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 04-14-2001 12:51

Hmm, I'm not really sure that that is a PHP issue, because the problem is more on the client-side (i.e. that's how forms work). So, IMO the best way would be to generate unique names (as you've mentioned above) for submit buttons (i.e. NAME="submit1..n") and use something like this to find out which button has been pressed:

<? for ($i = 0; $i < 10; $i++) {
&nbsp;&nbsp;&nbsp;&nbsp;if (${"submit$i"}) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $i;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;}
} ?>

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 04-14-2001 22:41

Just to confirm linear's surmise about brackets in the name attribute, I know it works, I've done it with PHP.

The rest of this conversation is a little over my head yet, so I'll just shut up now.

Bye

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 04-15-2001 00:36

Personally, I think you hit the best solution.

One other thing that might work.
Use an image for submitting
<input type="image" src="/images/go.gif" value="Search" border="0">

Seems in my experience the value here is irrelevant if you put a bunch they will all submit the form.

I don't know if this will wiork but it might, and would fix the no-JS browser issue
personally and profesionally though, I no longer deal with no-JS enabled browsers.
First I haven't really looked but I know it's not easy to turn off in IE and second in Netscape it also kills CSS if you disable Javascript. which means a whole mess of pages will look ugly.

my .02



Walking the Earth like Kane

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-16-2001 08:05

That's exactly what I had done Max, but the fact that it was a O(n) operation just to determine a value which should be able to be passed was disturbing me.

BTW, you are probably right that PHP can't do much about form behavior, but one solution I thought of is the ability to include names AND values in the name field of a form element, even more than one could come in handy. For example the syntax could be much like it is in a URL:

<button type="submit" name="var1=2&var2=3" value="Submit me!">

Then it could check to see if there was an = in the name attribute and if so discard the value information.

Oh yeah, and I tried using an image for a submit button as well, but it didn't seem to pass variables that way. (Probably because image buttons aren't supposed to have a value).

« BackwardsOnwards »

Show Forum Drop Down Menu