-1

So, using PHP, I'm trying to make a simple poll for my website and I've hit a block which I can't seem to get around.

To explain this as short as possible, I've got a form, in which I'm displaying the values from the database, and it all works fine. Using a post method, I thought I'd submit the data to the next page, here's the troublesome code:

echo "<tr><td><input type='submit' name='glasaj' value='Glasaj'></td>";
echo "<td><input type='submit' name='rez' value='Rezultati'></td></tr>";

Basically, just two submit buttons in a form using a post method, so the problem occurs when I try to get the attribute names in the php file where the data is being submitted:

$glasanje = $_POST['glasaj'];
$rezultati = $_POST['rez'];

And the errors the PHP outputs are:

Notice: Undefined index: glasaj in C:\wamp\www\WebProg\79_a.php on line 7

Notice: Undefined index: rez in C:\wamp\www\WebProg\79_a.php on line 8

What did I do wrong here?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • 1
    ^ better yet, what does print_r($_POST) say :p – skrilled Dec 05 '13 at 19:07
  • 4
    ^ better yet, what does `var_dump($_POST)` say :p – SamV Dec 05 '13 at 19:08
  • The same thing. Problem is in the index that I passed in the post method, which is undefined for some reason. – user3071639 Dec 05 '13 at 19:08
  • 1
    @karthikr it should be `var_dump($_POST)` or `print_r($_POST)` – M Khalid Junaid Dec 05 '13 at 19:08
  • 1
    You are saying the error on print_r or var_dump is that $_POST is not defined? I don't believe you (unless you are in a framework of some sort) :p – skrilled Dec 05 '13 at 19:10
  • 1
    is your form type POST ? – GGio Dec 05 '13 at 19:12
  • What's the difference? I mean, echo, print_r, they all use the same 'glasaj' index and they're all going to output the same issue. What I'm wondering is, why it would do so, there seems to be some issue with submitting the data between the pages, the attribute name for the said button isn't being submitted to 79_a.php, right? – user3071639 Dec 05 '13 at 19:12
  • Removed [tag:mysql] tag. This question has nothing to do with databases. "Index" in the error message refers to array keys, not database indexes. – Bill Karwin Dec 05 '13 at 19:13
  • are you submitting to the correct URL? can you post full form html so we see what is going on? Are you trying to access these variables inside a function that is used inside another function that gets submitted to? – GGio Dec 05 '13 at 19:14
  • That's the form tag with it's attributes. – user3071639 Dec 05 '13 at 19:17
  • I solved it, firstly, the problem was that it wasn't the name attribute that was being passed through, but it was the value of the buttons, so I just changed that up. But, as MattDiamant mentioned, I cannot have two submit buttons, so I just checked if they're clicked with isset method, and if they are, only then would I pass them the values. Here's the code bellow, if anyone's interested or ever bumps into this thread somehow with the same issue: if(isset($_POST['glasaj'])){ $odgovor = $_POST['glasaj']; } if(isset($_POST['rez'])){ $rezultati = $_POST['rez']; } – user3071639 Dec 05 '13 at 19:27

1 Answers1

1

I can't tell for sure, because you didn't include your whole form, but this is my best guess:

You're using multiple submit buttons, and only the one you click on to submit the form will be included in your POST, and if you use another button to submit, then neither of them will be included. But, if you click <input type='submit' name='glasaj' value='Glasaj'>, you SHOULD see glasaj => Glasaj in your $_POST variable. If not, then we'll need more information about how you're submitting your form, and where you're trying to access these variables inside of $_POST.

MattDiamant
  • 8,561
  • 4
  • 37
  • 46