radar00 Posted October 22, 2007 Share Posted October 22, 2007 I am new to the board, and there are a lot of posts here so I am apologizing now if this question has been asked/answered a million times, but I do not even know how to begin a search for the answer. Here is what I have... <form action="XXX.php" method="post"> <input type=text name=text1> <input type=button name=button1 onclick="submit()"> <input type=button name=button2 onclick="submit()"> </form> My question is... in the XXX.php document, is there a way I can tell, which button was clicked? This question may be more HTML or JavaScript related than PHP, but I was uncertain where to post it. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/ Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 http://www.phpfreaks.com/forums/index.php/topic,164469.msg722038.html#msg722038 Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375241 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 I tried that, but I don't think it works due to the fact the type is a button and not submit. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375242 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 as long as you assign a name to it the if statement takes that name to check if that button was clicked.. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375243 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 since you are new the search is at the top with the banner and once you have solve your problem you click on the solve button Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375244 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 Alright. Thanks. Maybe my button names are something crazy that I didn't intend. I am using a count variable to name them. So... to check for my error, how could I get the name of the button clicked to print on the screen within onclick()? <input type=button name="(unknown)" onclick="document.write(...)"> What would replace the ... to display the name of the button name, which is defined, but hypothetically unknown? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375251 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 this.name Answered my own question. Sorry for the dumb question. I was just drawing a blank. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375253 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 there is no such thing as a dumb question. every question counts towards greater knowledge. except for those real dumb questions..lol but yeah that is DHMTL that you are using. to do it with php you would have to add a value attribute to your button so lets say that you have 3 buttons <input type=button name="unknown" value='1' onclick="document.write(...)"> <input type=button name="unknown" value='2' onclick="document.write(...)"> <input type=button name="unknown" value='3' onclick="document.write(...)"> and to check it it would be if(isset($_POST['unknown'])){ print $_POST['unknown'];// PRINTS OUT THE NUMBER } Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375257 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 unknown wasn't actually the button name. I was saying hypothetically if we didn't know the button name. Alright. This is frustrating me. I will just post the actual code because it is not working as I want. editSpaces.php if ($_POST['delete']) { echo "Record Deleted"; } else { echo "Record Saved"; } manageSpaces.php while($row = mysql_fetch_array($spaces)) { echo '<form action="editSpaces.php" method="POST">'; echo "<input type=text disabled=1 name=address value='" . $row['address'] . "' /> "; echo "<input type=text disabled=1 name=details value='" . $row['details'] . "' /> "; echo "<input type=text disabled=1 name=cost value='" . $row['cost'] . "' /> "; echo "<input type=text disabled=1 name=contact value='" . $row['contact'] . "' /> "; echo "<input type=button name='save_" . $count . "' value='Save' onClick='submit()' disabled=1> "; echo "<input type=button name='delete' value='Delete' onClick='submit()'>"; echo "</form>"; $count++; } There are some inconsistencies (naming of two buttons) in the code from me just guessing and checking to try to solve this. $count is defined at 0 before the while loop. The save button's disabled is set to 0 when another action occurs. Any clue where I'm going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375258 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 why don't you give the record id threw a hidden field? like that when you press delete you look for the hidden field as the source for the id you will delete. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375260 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 here is an example <?php if(isset($_POST['delete'])){ // delete from table where record_id=$_POST['record_id'] }elseif(isset($_POST['save'])){ // update table set record_id=$_POST['record_id'] } while($row = mysql_fetch_array($spaces)) { echo '<form action="editSpaces.php" method="POST">'; echo "<input type=text disabled=1 name=address value='" . $row['address'] . "' /> "; echo "<input type=text disabled=1 name=details value='" . $row['details'] . "' /> "; echo "<input type=text disabled=1 name=cost value='" . $row['cost'] . "' /> "; echo "<input type=text disabled=1 name=contact value='" . $row['contact'] . "' /> "; echo "<input type=button name='save_" . $count . "' value='Save' onClick='submit()' disabled=1> "; echo "<input type=button name='delete' value='Delete' onClick='submit()'>"; //add the hidden field to store the record number echo "<input name=\"record_id\" type=\"hidden\" id=\"record_id\" value=\"{$row['record_id']}\" /> "; echo "</form>"; $count++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375263 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 I'm still not getting it to go to the "// delete from table where record_id=$_POST['record_id']" part, and that is the issue I am having. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375266 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 can you post your entire code? you can remove your real table names if that an issue. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375267 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 manageSpaces.php <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>New Page 1</title> <script type="text/javascript"> function disable(form) { form.address.disabled = 0; form.details.disabled = 0; form.cost.disabled = 0; form.contact.disabled = 0; } </script> </head> <body> <form action="addSpaces.php" method="post"> Address: <input type="text" name="address" /> Details: <input type="text" name="details" /> Cost: <input type="text" name="cost" /> Contact: <input type="text" name="contact" /> <input type="submit" /> </form> <?php $con = mysql_connect(...); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db(...); $spaces = mysql_query("SELECT * FROM spaces"); $count = 0; while($row = mysql_fetch_array($spaces)) { echo '<form action="editSpaces.php" method="POST">'; echo "<input type=text disabled=1 name=address value='" . $row['address'] . "' /> "; echo "<input type=text disabled=1 name=details value='" . $row['details'] . "' /> "; echo "<input type=text disabled=1 name=cost value='" . $row['cost'] . "' /> "; echo "<input type=text disabled=1 name=contact value='" . $row['contact'] . "' /> "; echo "<input type=button name='save_" . $count . "' value='Save' onClick='if (confirm(\"Are you sure you want to save the changes?\")){disable(this.form);submit()}' disabled=1> "; echo "<input type=button name='edit_" . $count . "' value='Edit' onClick='disable(this.form);this.form.address.disabled=1;this.disabled=1; save_" . $count . ".disabled=0'> "; echo "<input type=button name='delete' value='Delete' onClick='if (confirm(\"Are you sure you want to delete this space?\")){disable(this.form);submit()}'>"; echo "</form>"; $count++; } mysql_close($con); ?> </body> </html> editSpaces.php <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>Filename</title> <script type="text/javascript"> </script> </head> <body> <?php $con = mysql_connect(...); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db(...); if (isset($_POST['delete'])) { // mysql_query("DELETE FROM spaces WHERE address='" . $_POST[address] . "'"); echo "True"; } else { // mysql_query("UPDATE spaces SET details='" . $_POST[details] . "' cost='" . $_POST[cost] . "' contact='" . $_POST[contact] . "' WHERE address='" . $_POST[address] . "'"); } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375268 Share on other sites More sharing options...
roopurt18 Posted October 22, 2007 Share Posted October 22, 2007 echo "<input type=button name='save_" . $count . "' value='Save' onClick='submit()' disabled=1> "; echo "<input type=button name='delete' value='Delete' onClick='submit()'>"; I just wanted to point out that the reason $_POST['delete'] is not set is because you set the type attribute to button. If you want the button name to appear in $_POST the input type has to be submit <input type="submit" name="a_button" value="Go" /> Why in the heck are you using Javascript to submit the button anyways? You do realize if someone comes along with JS disabled they won't be able to use your form, right? I'm going to watch a movie, I'll check this again in the morning. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375270 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 That is what I said in my 2nd post, and I don't know how else I should do it without JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375272 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 And I wanted the confirm message Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375273 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 what the guru said i just realized you didn't have the submit as a type att. take out the onclick and on type put submit remove button Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375274 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 Alright. That works, but I want a confirmation before deleting the record from the table. Any recommendations? Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375275 Share on other sites More sharing options...
corillo181 Posted October 22, 2007 Share Posted October 22, 2007 use a JavaScript alert but it wont work if someone has it off. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375277 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 echo "<input type=submit name='delete' value='Delete' onClick='if (confirm(\"Are you sure you want to delete this space?\")){disable(this.form);submit()}'>"; That won't work though with the type as a submit. Even if the user hits cancel, it will still submit. I'm teaching myself PHP / MySQL, so I am not really sure all I have available yet. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375279 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 Where am I screwing up on the updating? mysql_query("UPDATE spaces SET details='" . $_POST['details'] . "' cost='" . $_POST['cost'] . "' contact='" . $_POST['contact'] . "' WHERE address='" . $_POST[address] . "'"); mysql_query("UPDATE spaces SET details='" . $_POST['details'] . "' AND cost='" . $_POST['cost'] . "' AND contact='" . $_POST['contact'] . "' WHERE address='" . $_POST[address] . "'"); Neither would work. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375291 Share on other sites More sharing options...
radar00 Posted October 22, 2007 Author Share Posted October 22, 2007 Nevermind. I found an example. Just missing commas. Quote Link to comment https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.