i am a php beginner and have a question for you please.
i have a text file with list of first and last names like this:
john,smith
sam,lewis
david,davidson
mike,anderson
in my sort.php file, it sorts that name list by first name in ascending order with index number, which will display like this:
1. david,davidson
2. john,smith
3. mike,anderson
4. sam,lewis
also, in that sort.php file, there's a form for input type=text
where you can type index number and a button to delete that index number entered in that text field:
<form action="deletename.php" method="post">
<table>
<tr valign="top">
<td>Delete: <input type="text" size="2" name="indexnumber" />
</td>
<td>
<div align="left">
<input type="submit" name="delete" value="Delete" />
</div>
</td>
</tr>
</table>
</form>
now the question...is there anything i can do in that form to send the value of that index number i entered in the text field? in other word...if i enter 3, what can i do to that form to send the value "mike,anderson" to deletename.php?
thanks in advance.
When using forms you can specify how the variables are sent to your script using either GET
or POST
, this is definied in your <form>
tag. You have choosen to use a POST method (sensible). POST method hides the information from the user where as GET method uses the website's URL to pass variables, eg /deleteme.php?indexnumber=2
The values will then becomes readable on deletename.php (because thats where your submitting your form to) by accessing the $_POST
array. If you entered 2 in your form you can read the value like so.
echo $_POST['indexnumber'];
/* OUTPUT */
2
Using this id, it will be up to you to find out which index number this id matches with. If you were using a database, this would be alot efficient and easier to maintain. I suspect you need to read some tutorials on PHP before going any further.
You don't need to change the form for this (you already have the field for the index number in place), but in deletefile.php, you have to read out what the form is sending, and delete that index number from the text file.
In addition, because you're ordering the names in sort.php, you'll need to repeat the exact same sorting in deletefile.php so that you know which index number corresponds with which entry. Then you just delete the corresponding entry.
So in deletefile.php, to process the form submit, you could so something like
if(!empty($_POST['indexnumber'] && is_numeric($_POST['indexnumber'])) {
//now do the sorting, e.g. include sort.php which I assume returns a sorted array
//then delete $_POST['indexnumber'] from the array and update the textfile
}
If you can't make anything of this, please post your current sort.php code so that I can see what it does.