Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with form buttons 1

Status
Not open for further replies.

bedrock

Programmer
Nov 6, 2002
94
US
ok so i have a select box inside a form and need to have two buttons (preferably images). clicking one button will move the selected element up in the list, and the other will move it down. my problem is that i cant figure how to distinguish between which button is pressed. does the button name show up anywhere in the php vars?
 
yeah originally i wanted to do without refreshing but its not really that important. so now im submitting the form to the same page to do all the "magic". im pretty flexible on the buttons, i mainly just want this to work. id even use anchors if needed. right now im using input type="image". mainly i just want to be able to reorder the list. im not too picky on how i go about it.
 
I'd suggest heading over the javascript forum with this question, you can handle it much more elegantly there.

But for the sake of argument, the way I've handled determining which buttoned is pressed in PHP is to have the button press call a javascript function and pass it a variable, then set some hidden field to that variable or somesuch.

i.e.
Code:
<input type=&quot;button&quot; name=&quot;Press me&quot; value=&quot;button1&quot; action=verify_form(this.value)>

then somewhere in the header
function verify_form(which_button) {
  document.formName.hiddenfieldname.value = which_button;
  document.formName.submit();
}

Save yourself some hassle, and make absolutely certain you don't have any form elements named submit.

-Rob
 
i dont know much javascript and unfortunately have just been avoiding it. for now i think ill stick with passing the variable along. one of these days ill spend some more time on the &quot;elegance&quot; and learn some javascript. thanks bunchs for the advice. what we see depends mainly on what we're looking for.
--unknown
 
Well, in case you're new to javascript completely... up in the head you're going to need some more... as in..

<script language=javascript>
stuff from above
</script>

-Rob
 
I don't have any of my HTML references with me right now, but I recommend you experiment by using different kinds of buttons, and performing &quot;print_r($_POST);&quot; to see what is returned.

You will find that you should be able to deduce with of a pair of buttons was clicked. For example, if you click on an image to submit a form, you not only can tell on which image, but where (in x and y coordinates) on the image you clicked. Want the best answers? Ask the best questions: TANSTAAFL!
 
hmm well i still seem to be having problems. heres what i have so far...

Code:
/*********************
//this part tests if the form was submitted and does the reordering
if($_POST[&quot;group&quot;]){
  if($_POST[&quot;direction&quot;]==&quot;up&quot;){
    //move up
  }
  else{
    //move down
  }
}

//the function to post the form with added var
<head>
<script language=&quot;javascript&quot;>
  function do_form(which_button){
    document.change_order.direction.value = which_button;
    document.change_order.submit();
  }
</script>
</head>

//input form
<form name=&quot;change_order&quot; action=&quot;edit.php&quot; method=&quot;post&quot;>
  <input type=&quot;hidden&quot; name=&quot;direction&quot;>
  <select name=&quot;group&quot; size=&quot;<? print $_SESSION[&quot;num_groups&quot;]?>&quot;>
    //<option tags are php generated by querying a mysql db
  </select>
  //buttons
  <input type=&quot;image&quot; name=&quot;up_button&quot; value=&quot;up&quot;
    action=&quot;do_form(this.value)&quot; src=&quot;images/up.jpg&quot;>
  <input type=&quot;image&quot; name=&quot;down_button&quot; value=&quot;down&quot;
    action=&quot;do_form(this.value)&quot; src=&quot;images/down.jpg&quot;>
</form>
*******************/

clicking either button just prints &quot;down&quot;. i think i have a pretty good idea on basic javascript now but im at a loss regardless. i tried adding a line document.write(which_button) to function do_form but it didnt print anything out. so either i cant output at that point or im passing the var wrong. what we see depends mainly on what we're looking for.
--unknown
 
i completely agree sleipnir, i think you posted that while i was cutting and pasting code. if php will tell me what image was clicked that is all i need. ill check that out, and thanks again to both of you for your help what we see depends mainly on what we're looking for.
--unknown
 
I'm all for sleipnir's suggestion, I just didn't know those off hand, and have found for my uses the javascript method is a little better (you can add all sorts of error checking and the like in there.).

If you wanna go with the javascript solution, here are some pointers...

1.) don't bother with document.write... instead use
alert(which_button);
it will pop up a dialog box with whatever you put in there, if it's blank that means the variable is blank (as compared to undefined).

2.) instead of using this.value, use &quot;up&quot; and &quot;down&quot; when you call the function... so
<input type=button name=&quot;up&quot; action='do_form('up')'>

3.) if you still have questions keep asking, tomorrow I'll have access to my computer with the webserver so I can be more helpful instead of off the top of my head. (chances are I screwed up the this.value format just because of that).

-Rob
 
Note that JavaScript isn't really the solution if you want to be portable and reach a vast audience. //Daniel
 
thanks for the tips skiflier. i was using strings instead of (this.value), but since it wasnt working i was fooling around with different things. actually the only thing that was wrong with that code is i tested $_POST[&quot;direction&quot;]==&quot;up&quot; instead of &quot;up_button&quot;. but since then i went with sleipnir's idea and it worked great. i used images for the inputs and then in $_POST there were values for &quot;image_name_x&quot; and &quot;image_name_y&quot; which corresponded to the image and pixels that were clicked like he had said. and i stayed completely with php. thanks a million guys! what we see depends mainly on what we're looking for.
--unknown
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top