Jump to content

Using html buttons to call php scripts.


wpb

Recommended Posts

This is probably more of an HTML question than a PHP one, but as I encountered it in a PHP context, I thought someone here might be able to offer advice...

 

I have a MySQL database with records that each have a unique ID number. I use PHP to create a table listing all the records in the database, and I'd like to have an "Edit" and "Delete" button alongside each entry in the table.

 

Rather than just using a link like this:

 

<a href="editentry.php?id=1">Edit</a>

<a href="editentry.php?id=2">Edit</a>

<a href="editentry.php?id=3">Edit</a>

etc.

 

I was hoping to use an HTML button, as they stand out a bit more. As in:

 

<a href="editentry.php?id=1"><button>Edit</button></a>

etc.

 

But I don't seem to be able to make a button into a link. Is this a genuine limitation?

 

I know I could get this to work if I used forms, but I'd have to use a form for every single button in the table, as the ID numbers vary for each table row.

 

I don't want to use Javascript's onclick handler, and I'd rather not create my own image that looks like a button. Any other ideas?

 

Many thanks,

Link to comment
https://forums.phpfreaks.com/topic/79662-using-html-buttons-to-call-php-scripts/
Share on other sites

Hey wpb,

 

You'd do it like so.

 

<a href="editentry.php?id=1"><input type="button" value="Edit"></a>
<a href="editentry.php?id=2"><input type="button" value="Edit"></a>

 

And so on. I didn't test that but I think that's how you do it. :D

 

You can also the use different images as buttons too.

 

Sam

More a self-imposed limitation than a genuine one.

 

"I don't want to use any method that works (like checkboxes, javascript, images, multiple forms) so how can I do this?"

 

The only option left that I can think of is make them all input type=submit buttons with the same name but give them all uniique values so you will know which one was clicked.

 

[pre]

[ Edit  998] [Delete  998]

[ Edit  999] [Delete  999]

[ Edit 1000] [Delete 1000]

[/pre]

 

where the numbers are the record ids or some other unique column such as username

Thanks for the replies.

 

Unfortunately, helraizer's suggestion doesn't work (not in IE, anyway). I thought I could just wrap a <button> tag up in a <a href> link, but it just doesn't seem to work.

 

I realize it seems like I'm being contrary, Barand, but I don't want to use Javascript as my browser doesn't support it, and I was hoping to avoid putting a seperate form around every single button to cut down on the byte size of the page. Equally, it seems a shame to have to create a button png or something, when there's a perfectly good one in HTML 4 anyway with the <button> tag. Just a shame you don't seem to be able to make it into a link.

 

Cheers,

 

Will

the <button> tag is taking the click event so you need to deal with that here is one way

 

test.php

<?php
if (isset($_REQUEST['test'])) {
echo "test = ".$_REQUEST['test'];
}
?>
<button onclick="location = 'test.php?test=1'">Click Me!</button>

I'd use something like this, allowing only a single record to be selected for edit, but many for deletion

<?php
$names = array (1=>'Peter', 'Paul', 'Mary');             // array to simulate db data

if (isset($_GET['sub']))
{
    switch ($_GET['sub'])
    {
        case "Edit":
            echo "Edit record id {$_GET['edit']}";
            break;
        case 'Delete':
            $del_list = join(',', $_GET['del']) ;
            $sql = "DELETE FROM atable WHERE id IN ($del_list)";
            echo $sql;
            break;
    }
}
?>
<form>
<table>
   <tr>
      <td>
         ID
      </td>
      <td>
         Name
      </td>
      <td>
         Edit
      </td>
      <td>
         Delete
      </td>
   </tr>
<?php
        foreach ($names as $id=>$name) 
        echo"   <tr>
              <td>
                 $id
              </td>
              <td>
                 $name
              </td>
              <td>
                 <input type='radio' name='edit' value='$id'>
              </td>
              <td>
                 <input type='checkbox' name='del[]' value='$id'>
              </td>
           </tr>";
?>
</table>
<input type="submit" name="sub" value="Edit">
<input type="submit" name="sub" value="Delete"> 
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.