M.chaudhry
M.chaudhry

Reputation: 651

Passing a value through Button to Php function

enter image description here

I am beginner in php and I am currently working on admin panel (you can see my admin panel page). The thing is that I want to pass serial number through these two buttons to perform further. But I can't find how to send $value to edit and delete a particular line.

<div id="headin2"><strong> <h3>Admin page </h3></strong></div>
<?php 
    echo "<table width=\"100%\" border=\"0\" id=\"tab\">";
    echo "<tr>";
    echo "<th id=\"td1\">Serial No</th><th id=\"td2\">Account Title</th>
              <th id=\"td3\">Email</th><th id=\"td4\">Gender</th><th id=\"td5\">city</th>
              <th id=\"td6\">Course</th><th id=\"td7\">status</th><th id=\"td8\" colspan=\"3\">Action</th>";
    echo "</tr>";

    while ( $row = mysql_fetch_array($query))
    {
        $SN = $row['SN'];
        $actitle = $row['ac_title']; 
        $email = $row['email'];
        $gender  = $row['sex']; 
        $cite = $row['city'];
        $course = $row['CRS'];
        $status  = $row['status'];  

        echo "<tr>";
        echo "<td>".$SN."</td><td>".$actitle."</td><td>".$email."</td>
                  <td>".$gender."</td><td>".$cite."</td><td>".$course."</td><td>".$status."</td>
                  <td>"."<input type=\"button\" name=\"edit\" value=\"Edit\"/>
                  <input type=\"button\" value=\"Delete\" name=\"delete\" >"."</td>";
        echo "</tr>";
    }
?>

</table>

Upvotes: 8

Views: 83835

Answers (6)

Abdullah
Abdullah

Reputation: 11

<a href=register_course.php?id=$roll&code=".$row['course_code']."&name=".$row['course_name']."><input type=submit value=Register>

*here register_course.php is the file where you are sending some variable having some data it will be delivered when button is clicked whose value is register //course code is the variable you want to send ... im assigning its value as $ code... and assigning id $roll as id and assigning course_name as $name....

the other file will get the variables having some data

Upvotes: 1

Zahidul Hossein Ripon
Zahidul Hossein Ripon

Reputation: 672

when you click Edit it open a page with corresponding record. You can do it by a Edit link or a image or a button.

<?php echo "<a href='".BASE_URL."admin/edit.php?id=$id' target='_blank'> <img src=".BASE_URL."/images/edit.png width=16 height=16 alt=Edit /> </a>";?>

for delete you can use jquery. here is a example

echo "<a href='#' onclick='deletePrescription($id)' ><img src=images/document_delete.png width=16 height=16 title='Delete Prescription' alt=Delete /> </a>";    
<script type="text/javascript">
function deletePrescription(checkup_id) {
//alert(checkup_id);                
var agree=confirm("Do you really want to delete the prescription?");
    if (agree)
        {
            $.ajax
            ({
                type: "POST",
                url: "delete_prescription_admin.php",
                data: "checkup_id="+checkup_id,
                success: function(msg)
                {
                //
                //alert( "array Updated: " + msg );
                location.reload(true);
                }
            });

        }
    else
        {
        return false ;
        }

      }
     </script> 

Upvotes: 3

AlexP
AlexP

Reputation: 9857

You need both the action (edit/delete) and the row id. Unfortunately, without some JS the button will only post one value.

You can create a new form for each row, add in a hidden element. For example:

<?php while ($row = mysql_fetch_array($query)) : ?>

  <tr>
    <!-- other cells -->
    <td>
      <form method="post" action="">
        <input type="submit" name="action" value="Edit"/>
        <input type="submit" name="action" value="Update"/>
        <input type="hidden" name="id" value="<?php echo $row['id']; ?>"/>
      </form>
    </td>
  </tr>

<?php endwhile; ?>

Then after posting it you can just check for the action and id

if ($_POST['action'] && $_POST['id']) {
  if ($_POST['action'] == 'Edit') {
    // edit the post with $_POST['id']
  }
}

Upvotes: 11

Gal Samuel
Gal Samuel

Reputation: 493

PHP is on the server side, so you need to send parameters, parse it and have your php act. i.e.

The edit button will make POST or GET request with parameter: id=123&action=edit

and your PHP will parse the Query String:

$strID = $_GET['id'];
$strAction = $_GET['action'];

then act on it..

if ($strAction=='edit'){
 // do something...
}

Your buttons should be wrappen in a form like:

<form action="yourFile.php?id=<?=$SN ?>&action=edit" method="GET" id="formEdit<?=$SN ?>">
    <button type="submit" value="Edit">
</form>

Upvotes: 2

David
David

Reputation: 3822

You can do it one of two ways.

jQuery and AJAX

For each <tr>, everywhere there is a delete button,

<a href="#" data-id="<?php echo $value; ?>" class="delete-row">Delete</a>

Script at the bottom:

//Include jQuery here
<script language="javascript">
$('.delete-row').click(function (event) {
   event.preventDefault();
   var id = $(this).data('id');
   $.ajax({
      url: "url/to/delete",
      method: "POST",
      cache: false,
      data: { id: id },
      success: function (html) {
         $(this).parent().parent().remove();
      });
   });
});
</script>

This puts the ID of the row into the <a href> itself using data and uses jQuery to send out an AJAX call to delete the record. If the delete is successful, it removes the row from the table.

Old-School Button

For each <tr>, everywhere there is a Delete button,

<form method="POST" action="url/to/delete">
<input type="hidden" name="id" value="<?php echo $value; ?>" />
<input type="submit" value="Delete" />
</form>

This is the old-school way to do it, where the hidden field is how the backend knows which row to delete.

On the backend, you still use $_POST['id']; to get the ID of the record to remove. In the above examples, $value is the ID for each row, and is most likely something like $row['id'] when it is coming from a foreach().

Upvotes: 7

Jan Heinrich Reimer
Jan Heinrich Reimer

Reputation: 781

Use a hidden input (e.g.<input type="hidden" value="your_value_here">)

Upvotes: 3

Related Questions