freephoneid Posted September 17, 2008 Share Posted September 17, 2008 Hi, We're maintaining existing php code written using smarty templates. I've a page a.php which has 2 buttons Add & Delete. The action is set to b.php. Now, in b.php, the code performs actions as shown below: a.php: ==== <form action="b.php"> ..... <input type="submit" name="add" value="Add"> <input type="submit" name="delete" value="Delete"> ..... </form> b.php ==== if (isset($_POST['addn']) ) { // Perform add action & put the data into DB } else if (isset($_POST['delete']) ) { // Perform delete action & put the data into DB } Now the above code is the existing code in Production & works fine. However it does not work when someone uses Enter key in IE & expects that the item will be added. The control goes to b.php but since the post variable is not present, it does not do anything & does not add to the database. Can anyone tell me how to solve this issue? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/ Share on other sites More sharing options...
BlueSkyIS Posted September 17, 2008 Share Posted September 17, 2008 if (isset($_POST['delete']) ) { // Perform delete action & put the data into DB } else if ($_SERVER['REQUEST_METHOD'] == "post") { // do this for any other post. // Perform add action & put the data into DB } Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644280 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Or you could use a javascript function with an event listener that either submits the page how you want it to when they hit enter, or make it not submit at all when they hit enter. You could even add an alert that tells them to click either update or delete. Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644281 Share on other sites More sharing options...
freephoneid Posted September 17, 2008 Author Share Posted September 17, 2008 But I've lot of if else conditions in b.php checking for different buttons....Actually, I've 10 buttons calling the same file b.php & I've all if else conditions. If I use the POST check, then all other button functionality will fail....Also, we are not allowed to use javascript.... Any other soln or idea? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644286 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 A work around could be to add a hidden input that is unique to that page/form that you could check for. <form action="b.php"> ..... <input type="submit" name="add" value="Add"> <input type="submit" name="delete" value="Delete"> <input type="hidden" name="submitted" value="1"> ..... </form> b.php ==== if (isset($_POST['addn']) ) { // Perform add action & put the data into DB } else if (isset($_POST['delete']) ) { // Perform delete action & put the data into DB } else if (isset($_POST['submitted']) ) { // Perform the "hit enter button" function } Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644287 Share on other sites More sharing options...
freephoneid Posted September 17, 2008 Author Share Posted September 17, 2008 Hi every1, Thanks again for the quick reply. However, with this approach, I'll have to replicate the code in if else loop. Basically, I need the enter key bahaviour same as clicking Add button. Even if I do the OR condition (if ADD or submitted), add will work fine but delete functionality will also go to the add condition. Stillll Any other idea to solve this? Thanks Again! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644291 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Javascript would be perfect for this. In any case, would this help: <form action="b.php"> ..... <input type="submit" name="add" value="Add"> <input type="submit" name="delete" value="Delete"> <input type="hidden" name="submitted" value="1"> ..... </form> b.php ==== if (isset($_POST['addn']||(!isset($_POST['delete'])&&isset($_POST['submitted']))) { // Perform add action & put the data into DB } else if (isset($_POST['delete']) ) { // Perform delete action & put the data into DB } Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644294 Share on other sites More sharing options...
freephoneid Posted September 17, 2008 Author Share Posted September 17, 2008 Hi, Thanka again for the reply! However, even this one will not work since the POST['delete'] is always true. When I'm printing the POST array, the delere is always present in the POST. I don't know why but when I hit enter, delete is always present...Here is the code for delete: <button name="delete" type="submit" class="databutton" value="Delete"> <img src="images/icon_delete.gif" alt="Delete" style="vertical-align: middle;" border="0">Delete</button> Any idea/soln? Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644296 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 You should use input rather than button. Also, why don't you check and see if both add and delete are present, and if they are, assume they hit enter? Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644298 Share on other sites More sharing options...
freephoneid Posted September 17, 2008 Author Share Posted September 17, 2008 Hi, The reason behind using button for Delete is that we're displaying the Delete button with image on it. Because of this, the delete is always present in POST. However since add does not have any image, it is not present in POST when enter is hit..... Any idea or super duper soln? Thanks again for reply!! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644304 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 In that case, you should use the input type="image" rather than button. Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644305 Share on other sites More sharing options...
freephoneid Posted September 17, 2008 Author Share Posted September 17, 2008 If I use type = image, then how it'll get submitted? I mean if you look at the syntax, <button name="delete" type="submit" value="Delete"> <img src="images/icon_delete.gif" alt="Delete" style="vertical-align: middle;" border="0">Delete</button> I'm using type="submit", can you provide me complete syntax? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644307 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 <input type="image" name="delete" value="Delete" src="images/icon_delete.gif" alt="Delete" style="vertical-align: middle;"> image type inputs act exactly the same as submit types. Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-644311 Share on other sites More sharing options...
freephoneid Posted September 18, 2008 Author Share Posted September 18, 2008 Hi, This will display only the image. However, the reason we used <button...was because using button, we were displaying actual button with image beside it...... Do you have any soln to display the regular button along with the image beside it. The image must be on the button. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645021 Share on other sites More sharing options...
F1Fan Posted September 18, 2008 Share Posted September 18, 2008 Sorry, but the button tag is not supported very well, and all the browsers treat the button differently. I suggest you do one of three things: 1) Just use the image input type and forget the button 2) Just use the submit input type and forget the image 3) Have an image input type and a submit (for your button) The problem with the third option is that you would have to name them each something unique, and then check for that in your PHP. Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645076 Share on other sites More sharing options...
Zane Posted September 18, 2008 Share Posted September 18, 2008 is it so crucial that you use POST for this it would be much less of a pain for you if you just did this with GET you wouldn't even need a form.......of course I still don't understand what you need ENTER for..but still for instance and while you're at it....why do you need so many Ifs and elses for b.php you could just have a switch statement do all the work for you.....especially with the above method. switch($_GET['action']) { case 'add': //Do add stuff break; case 'del': //Do delete stuff break; case 'edit': break; case 'whatever': break; default: break; } Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645121 Share on other sites More sharing options...
freephoneid Posted September 18, 2008 Author Share Posted September 18, 2008 ok......I'll go with the <input type="image" name="abc" approach.....however, in POST why it appears as ['abc_x'] & ['abc_y']???? Can you please guide me how should I handle in POST? Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645173 Share on other sites More sharing options...
Zane Posted September 18, 2008 Share Posted September 18, 2008 # The image submit button sends both the value of the submit button and the x and y coordinates where the button was clicked. Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645184 Share on other sites More sharing options...
freephoneid Posted September 18, 2008 Author Share Posted September 18, 2008 So does it mean that I have to change my existing code from ....... } else if (isset($_POST['delete'])) { ...... to ....... } else if (isset($_POST['delete_x'])) { ...... There is no way, it'll work with above existing condition? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645191 Share on other sites More sharing options...
Zane Posted September 18, 2008 Share Posted September 18, 2008 the initial way should still work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645198 Share on other sites More sharing options...
freephoneid Posted September 18, 2008 Author Share Posted September 18, 2008 Hi, .... else if (isset($_POST['delete'])) { .... is not available in POST & I've defined the button as <input type="image" name="delete" value="Delete" src="images/1.gif" alt="Delete"> ......Can anyone help me, why???? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645208 Share on other sites More sharing options...
freephoneid Posted September 19, 2008 Author Share Posted September 19, 2008 Hi, Can anyone tell me... if (isset($_POST['delete'])) { .... is not available in POST & I've defined the button as <input type="image" name="delete" value="Delete" src="images/1.gif" alt="Delete"> Will the delete be avialable in POST or delete_x & delete_y? If I need delete, what should be done? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645741 Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 Like someone already said, the "_x" and "_y" are the coordinates that were clicked. You really should be getting three: delete delete_x delete_y If you don't see it, try doing a print_r($_POST); to see everything that you're getting. Maybe you'll see something you didn't before. Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645747 Share on other sites More sharing options...
freephoneid Posted September 19, 2008 Author Share Posted September 19, 2008 Nope......Here is my test program: a.php ==== <form name="aa" action="b.php" method="post"> <input type="text" name="xx" value="77"> <input type="image" name="deleteasn" value="Delete" src="images/1.gif" alt="Delete"> </form> b.php ==== <?php print_r($_POST); ?> And here is the result: Array ( [xx] => 77 [deleteasn_x] => 51 [deleteasn_y] => 13 ) Any idea why delete is not listed in POST? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645753 Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 OK, something is up. I just tested on my server. I made it all one file: <form name="aa" method="post"> <input type="text" name="xx" value="77"> <input type="image" name="deleteasn" value="Delete" src="images/1.gif" alt="Delete"> </form> <?php print_r($_POST); ?> and when I clicked the delete button, this is what I get: Array ( [xx] => 77 [deleteasn_x] => [deleteasn_y] => [deleteasn] => Delete ) Quote Link to comment https://forums.phpfreaks.com/topic/124732-php-code-with-enter-key/#findComment-645768 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.