Jump to content

php code with Enter key


freephoneid

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!!

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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;

}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 ) 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.