SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comparison of POST data

    No idea if I'm doing something blatantly wrong here, if someone could help I'd really appreciate it. I tried googling, but I have no idea how to really describe the problem without showing the code.

    I currently have a page where a user fills out one of three forms. They send the data off and depending on what form they sent an if statement on the destination page picks it up and works with the data.

    One of the forms send an variable called event to the new page.
    PHP Code:
    <input type="hidden" name="event" value="<?php echo htmlentities($events[$k-4], ENT_QUOTES); ?>" />
    It then tries to compare it like this.
    $events[#] is an already created array.
    PHP Code:
    $event $_POST['event'];

    if(
    $events[$i] == $event)
        {
            ...
    action
        

    And if it's the same thing, the if statement is true and it evaluates.

    However...

    If you send the data like this from the other two forms:
    HTML Code:
    <select name="event">
    <option value="March of Dimes">March of Dimes</option>
    <option value="Seamen's Society">Seamen's Society</option>
    <option value="Food Drive">Food Drive</option>
    <option value="YMCA">YMCA</option>
    </select>
    And try to evaluate it, it will never work.
    PHP Code:
    $event $_POST['event'];

    if(
    $events[$i] == $event)
        {
            ...
    action
        

    I don't know if I'm doing something obviously wrong here. I very well might be. I've been reading on sanitizing database input for security and I've been trying, but is that why my statement never works in the second situation? No matter what I do it never works. I tried making them both elements of an array - but nothing. Aren't they all identical strings in both cases? Why is one not working?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Have you tried printing out the contents of $event in your code and eyeballing whether it LOOKS equal to something in your array? Sometimes the problem is obvious.

  3. #3
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They look EXACTLY the same. And if I make a third variable like
    PHP Code:
    $this "March of Dimes" 
    And compare it where the user has selected March of Dimes from the drop down
    PHP Code:
    if ($this == $event)
    {
    ...
    actions
    }

    if (
    $this == $events[$i])
    {
    ...
    actions

    It does both sets of actions.

    I'm very confused. It's just that one if statement from my first post will never evaluate to true.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You say this condition is not working as you expected
    PHP Code:
    if($events[$i] == $event
    So, inspect the values closely
    PHP Code:
    var_dump($events[$i]);
    var_dump($event); 
    What do you get?

  5. #5
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like the one coming from the array had an extra space infront of it, but when you simply echo it out, there is no space. That was my first guess so I checked it last night, but the var_dump showed the space... odd.

    I took the front space off and now it works.

    Thanks for all the help!

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The space was there when you echo'd it, you just didn't notice it. Sometimes it's not obvious unless you view the html source output. Some characters aren't visible either, but since var_dump() shows you the number of bytes(characters), you can still catch that stuff.

  7. #7
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I don't know if that space was there, when I echo'd it I wrapped it around two periods .March of Dimes. so I could see if it was, and also, why would it work when i sent the data from the hidden input field? I'm not complaining that it's working I'm just really curious as to why it's happening. O_o


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •