Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a value through Button to Php function

Tags:

php

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>
like image 481
M.chaudhry Avatar asked Nov 06 '13 14:11

M.chaudhry


1 Answers

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']
  }
}
like image 60
AlexP Avatar answered Oct 13 '22 01:10

AlexP