PHP Sending parameter to sub procedure

Hi everyone,
It seems I dont understand the mechanism of sending a parameter from one PHP procedure to another.
Here is the code of the main procedure test.php:

<!DOCTYPE html>
<html>
<head>
  <title>Update Records In MYSQL Database Using PHP</title>
</head>
<body>
  <!--connecting to database-->
  <?php
    $con = mysqli_connect('aa', 'bb', 'cc', 'dd');
    IF(!$con)
      DIE('Gevald' .MYSQLI_CONNECT_ERROR());
    $records = mysqli_query($con, "SELECT * FROM stations");
  ?>
  <table>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
      </tr>
      <?php
        while($row = mysqli_fetch_array($records))
	{
	  echo "<tr><form action = 'test.php' method = 'post'>";
	  echo "<td><input type = 'text' name = 'mkt_id' value = '".$row['id']."'></td>";
	  echo "<td><input type = 'text' name = 'mkt_name' value = '".$row['name']."'></td>";
	  echo "<td><a href='test1.php?edit_mkt=".$row['id']."'>edit</a></td>";
	  echo "</form></tr>";
	}
      ?>
    </table>
</body>		
</html> 

The sub procedure (test1.php) to accept the parameter (mkt) goes like this:

<?php
  echo $edit_task;
?>

When I run the main “test.php”, I receive an error message from test1.php that says:
Notice: Undefined variable: edit_task in C:\xampp\htdocs\update\test1.php on line 17
Can anyone explain me why do I get that error and how to pass the parameter from test.php to test1.php?
Thanks

They are functions and methods. It doesn’t look like you have designed it in a way for that to be useful. test1.php is a file, not a procedure to pass parameters too. Now, if it was a command line application, you could, but not a web program.

Well, the anchor sends the “id” value to the second PHP program named test1.
If it just displays the “id” value, that is all you should see on the page. It does not because you never read the parm passed to it. You send it as test1.php?edit_mkt=SOME-VALUE.

BUT, in the second file, you never retrieve that value. You need to do the second file this way:

<?php
 $edit_task = $_GET["edit_mkt"];
 echo $edit_task;
?>

This will retrieve the value passed to it and allow you to use it in the new file. You need to remember that the test1.php file is an entirely separate PHP program. It needs to know where the info is coming from. In this case you passed it using a parameter or argument passed thru the anchor. Do you understand that?

2 Likes

Thanks a lot ErnieAlex !

Sponsor our Newsletter | Privacy Policy | Terms of Service