Page 1 of 1

problem with POST

Posted: Tue Mar 11, 2008 3:16 am
by borko
Recently I start to read about php and started my little task to make a simple program in which I would generate
two integers (each of them is in range of 0 - 50) and ask user to enter sum of these two numbers.
Program should check entered value, compare with correct value and print a message, but in may case it always print not set.
My problem is that no matter what I enter for sum my program get nothing as input.
Can someone tell me what I am doing wrong?

(I know that my english is bad but please have understanding.)

Here is my code:
<HTML><HEAD></HEAD>
<BODY>
<?php
// generating integers

for ($i=0;$i<=9;$i++){
$p[$i]=rand(0,50);
}


//print html tag <table>

echo "<table>";
for ($i=1;$i<=5;$i++){
echo "<tr><td><b>Zadatak $i.</b> Zbroj brojeva </td><td>".$p[$i-1]." i ".$p[$i+4]."</td> <td>iznosi : </td><td>&nbsp;";
echo "<input type=\"text\" name=\"zad.$i\" value = \"\" size = \"20\"></td>";
echo "</tr>";
}
echo "</table>";

//buttons for submit i reset

echo "<br><input type = \"submit\" name =\"submit\" value = \"Predaj na ocjenjivanje\"/>";
echo "&nbsp;&nbsp;";
echo "<input type = \"reset\" name = \"erase\" value = \"Brisanje svih unosa\"/>";
echo "</form>";
?>

<?php
}else{
//check if something is entered for first answer

if (isset($_POST['zad1'])){
$value = htmlspecialchars($_POST['zad1']);
echo "Entered value is ".$value;
}else{
echo "not set";
}

}
?>

</BODY>
</HTML>

Re: problem with POST

Posted: Tue Mar 11, 2008 4:08 am
by Sekka
Where's your <form> tag? You need to wrap all form fields within form tags or it won't get posted. E.g.

Code: Select all

<form id="myform" action="./" method="post"><!-- Form Content --></form>

Re: problem with POST

Posted: Tue Mar 11, 2008 4:18 am
by EverLearning
First, if you haven't left some part out of code you posted, it's not gonna work. You have several syntax errors in there, missing opening <form> tag, unmatching curly braces etc.... But on assumption that your code runs, the reason why you always get 'not set' response is because input field in the form are named 'zad.1', 'zad.2' ..., and in your code you're checking for $_POST['zad1'], which doesn't exist.
So to make it work change your form fields to

Code: Select all

echo "<input type=\"text\" name=\"zad$i\" value = \"\" size = \"20\"></td>";
.

You should have translated the text in your code , so that people can easily recognize what's its purpose. (Ne znaju svi tvoj maternji jezik :) )