tags:

views:

46

answers:

2

hi, 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.

A: 

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.

bigstylee
yes, i have << $indexnumber = $_POST["indexnumber"]; >> in my deletename.php.also, how do i use header() function in this file?i have no clue what it does and why i would need to use it.i saw some examples of how it's been used, but i couldn't understand how this function would effect my code and be useful in any ways.thanks.
tooepic
Any request on the website contains headers. This tells the application (typically a web browser) what type of data to expect (eg a web page). This is a rather simplistic view though and probably beyond your "need to know" at this stage. Saying that thought, headers can be used for redirection. header("Location: /sort.php"); at the end of deletename.php would send you back to your sort page. deletename.php must have no output though. Anyway, tutorials and google are your best friends now.
bigstylee
yeah, i've been reading tutorials and googles. even checking php.net and wiki...and few other sites. some sites have whole list of header(), but when i tried few example, i get warning: error. but yeah, i'll look into it more and see if i'll still scratch more where it doesn't even itch...lolthanks again.
tooepic
A: 

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.

bobsoap
original post was sort.php, but what if there's another php file that does reversort "rsort.php" that sorts in descending order and sends the variable to deletename.php? in deletename.php, i have:$indexnumber = $_POST["indexnumber"];in deletename.php, what can i do to know if the variable is from either sort.php or rsort.php so i can do either sort() or rsort() and delete the corresponding index array?Thanks.
tooepic
That's the general problem with your structure, it's really not scalable in a good way. The only workaround I see for this would be to add a hidden field to the form which reflects the current sort file. So in both sort files, you set a variable, e.g. $sortfile = 'sort' (or $sortfile = 'rsort'). Then you add that to the form like this: <input type="hidden" name="sort" value="<?php echo $sortfile; ?>" />. This field gets sent to deletename.php along with the indexnumber field, and you can read it out and include the correct sort file like so: include $_POST['sort'].'.php';. That would do it!
bobsoap
PS. always, always sanitize anything that a user inputs or that gets sent over post, get, and session, especially if you're doing it like above. Worst case scenario: someone would look at your source code, see the hidden field, and try to fill it with something like "http://bad-site.com/exploit". When that gets sent to your delete file and you're not checking/sanitizing the field, you'd end up getting "include 'h t t p ://bad-site.com/exploit.php';". Now if your PHP engine has the URL wrapper on, the sky will fall upon your site.
bobsoap
...so it would really be best if you just set $sortfile to 1 or 2, and in deletefile, you check for that field and if it's 1, you include sort.php, else you include rsort.php. Sorry for the long and multiple replies but there is a limit on the characters here. I hope this helps.
bobsoap
no, your detailed information is really helpful. i really appreciate it. though i haven't got to the include part yet, but i'll look into that and do what i can based on your provided information. thank you so much.
tooepic