Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP passing a variable from HTML to another PHP file

Tags:

html

php

mysql

Ok, so im making and admin page which requires to deletes entries from databases, and i cant figure out how to pass the id variable of the post to another php file.

echo '<form method="post" action="delete_member_post.php" style="text-align:center;" id="delete_post">';
echo '<p style="text-align:center; font-size:20px;"> <font font-size:20px face="Rockwell" color="#680000"> Select post to delete:</font>';
echo '<select name="delete" form="delete_post">';
for ($z=1; $z<=$text["id"]; $z++)
{
    $c=$c+1;
    echo '<option value="' . $c . '">' . $c . ' </option>';
}
echo '</select> <br>';
echo '<input type="submit" value="Submit Choice"=>';
echo '</form>';

After the loop in the PHP file im echoing $c (based on how many entries the DB has) and its printing my options.

In the other file im using

$i = $_POST["$c"];  

to get the id that had been chosen but all i get is the same error, Undefined variable and Undefined index.

Thanks for your time.

like image 378
DominusMors Avatar asked May 15 '26 06:05

DominusMors


2 Answers

$_POST['delete'] will return the value of the option that has been selected in the form.

From what I understand this is what you want, if the user has selected the ID 7 in the dropdown before submitting the form, $_POST['delete'] will contain the value 7 .

like image 159
Drown Avatar answered May 18 '26 19:05

Drown


I understand you want to send final value (count) of variable $c to another file? You should save the value of that variable in a hidden input field after the loop:

<input type="hidden" name="count" id="count" value="<?= $c  ?>" />

Now you can access it with

$i = $_POST["count"]; 
like image 27
DBWhite Avatar answered May 18 '26 19:05

DBWhite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!