/    Sign up×
Community /Pin to ProfileBookmark

Hidden input variable doesnt always pass to script

Hi,

I have been having problems getting a hidden variable in my form to submit to the new page. The variable is $average. My form has a checkbox, when this box is checked, $average will be empty on the new page, when it is not, $average will pass successfully.

Here is the relevant bit of code from my form script:

[code=php]
//$average is set to a value earlier in the script

<form action=’peer_assessmentDB.php’ enctype=’multipart/form-data’ method=’post’ name=’peer_marks’ onSubmit=’return equals100(this)’>
<strong>Equal Contribution </strong><input type=’checkbox’ name=’equalmarks’ onClick=’calcTotal(this.form); isChecked()’><br><br>
<table cellpadding=’8′ cellspacing=’0′>
<tr>
<th>Forename</th>
<th>Surname</th>
<th align=’center’>Mark</th>
</tr>”;

$result_reviewerPeer = mysql_query($sql_reviewerPeer,$connection) or die(‘Query failed: ‘ . mysql_error());
$num_reviews = mysql_num_rows($result_reviewerPeer);
for ($i=0; $i<$num_reviews; $i++) {
$row = mysql_fetch_assoc($result_reviewerPeer);
$reviewee=$row[‘Reviewee’];

$sql_reviewee = “SELECT * FROM $student_table
WHERE id = ‘$reviewee'”;
$result_reviewee = mysql_query($sql_reviewee,$connection) or die(‘Query failed: ‘ . mysql_error());
$reviewee_details = mysql_fetch_assoc($result_reviewee);

if ($i % 2 == 1) {
echo “<tr class=’evenRow’>”;
}
else echo ”
<tr>”;
echo ”
<td align=’left’>”.$reviewee_details[‘Forename’].”</td>
<td align=’left’>”.$reviewee_details[‘Surname’].”</td>
<td align=’center’>
<input type=’text’ class=’markBox’ name=’mark$i’ value='{$row[‘Mark’]}’ onClick=’calcTotal(this.form)’ onChange=’calcTotal(this.form); equals100(this.form)’>
</td>
</tr>”;
}

echo ”
<tr>
<td></td>
<td></td>
<td align=’center’><input type=’text’ name=’total’ class=’markBox’ disabled=’disabled’></td>
</tr>
<tr>
<td></td>
<td><input type=’hidden’ name=’average’ value=’$average’></td>
<td align=’right’><input type=’submit’ value=’Submit’ name=’submitbutton’></td>
</tr>
</table>
</form>”;

[/code]

Here is the part of the page that is called which gets the variable:

[code=php]
$average = $HTTP_POST_VARS[‘average’];
echo “average $average <br>”;

[/code]

If anyone can suggest where i might be going wrong, I’d be really grateful.

Many Thanks
Wendy

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NutterMar 08.2005 — Is there any JavaScript that runs when you click on the checkbox? Maybe there's a bug in there that's making the average blank.

How are you getting the average? Does it change based on what's entered into other boxes?
Copy linkTweet thisAlerts:
@cowjlauthorMar 08.2005 — [i]Originally posted by Nutter [/i]

Is there any JavaScript that runs when you click on the checkbox? Maybe there's a bug in there that's making the average blank.

How are you getting the average? Does it change based on what's entered into other boxes?[/QUOTE]


Hi, yes some JavaScript is called, but i don't think it is the problem as it otherwise functions ok and i get no errors (but i could well be wrong!). Also, I can check and uncheck the box (so that the javascript has been executed), and providing the box is unchecked when I submit, it works. Here is the javascript:

<i>
</i>function isChecked() {
if (document.peer_marks.equalmarks.checked == true) {
var limit = document.peer_marks.elements.length;
for (i=0;i&lt;limit;i++) {
document.peer_marks.elements[i].disabled = true;
}
document.peer_marks.equalmarks.disabled=false;
document.peer_marks.submitbutton.disabled=false;
document.peer_marks.total.value = 100;
}
if (document.peer_marks.equalmarks.checked == false){
var limit2 = document.peer_marks.elements.length;
for (i=0;i&lt;limit2;i++) {
document.peer_marks.elements[i].disabled = false;
}
document.peer_marks.total.disabled=true;
if (document.peer_marks.total.value != 100) {
document.peer_marks.submitbutton.disabled=true;
}
}
}

function calcTotal(f) { //JavaScript function to add the individual marks to a total
var totalscore=0;
var num_elements = document.peer_marks.elements.length;
var form_inputs = new Array();

<i> </i>var temp = num_elements-3; //the last 3 elements are not mark input boxes
<i> </i>for (i=1;i&lt;temp;i++) {
<i> </i> form_inputs[i] = Number(document.peer_marks.elements[i].value);
<i> </i>}
<i> </i>for (j=1;j&lt;temp;j++) {
<i> </i> totalscore += form_inputs[j];
<i> </i>}

<i> </i>if (document.peer_marks.equalmarks.checked == false) {
<i> </i> document.peer_marks.total.value = totalscore;
<i> </i>}
<i> </i>else {
<i> </i> var average = "&lt;?=$average?&gt;";
<i> </i> average = Number(average);
<i> </i> var num_elements = document.peer_marks.elements.length;
<i> </i> for (k=1;k&lt;(temp);k++) {
<i> </i> document.peer_marks.elements[k].value = average;
<i> </i> }
<i> </i>}
}


The average changes based on the number of people in a group (i.e. 4 people would be 100/4=25).

Many Thanks
Copy linkTweet thisAlerts:
@smercerMar 08.2005 — [i]Originally posted by cowjl [/i]

[B]

<i>
</i> var average = "&lt;?=$average?&gt;";

[/B][/QUOTE]

Theres your problem. Just remember that when on the client machine PHP code cannot be executed and has to be done on the server that serves the page to the client.

And I thought I was a beginner. but anyway you learn something every day.
Copy linkTweet thisAlerts:
@cowjlauthorMar 08.2005 — var average = "&lt;?=$average?&gt;";
Theres your problem. Just remember that when on the client machine PHP code cannot be executed and has to be done on the server that serves the page to the client.[/quote]


Hi,

yes you are right i am very much a beginnner at this ?

Can anyone enlighten me more as to what is wrong with that bit of code? As far as I can tell it sets the javascript variable to the value of the php variable. It appears to work as I use the javascript variable to populate the input boxes. This seems to work ok, it is the passing of the php variable in the following line of code that only works when the check box is not ticked:
[code=php]
<td><input type='hidden' name='average' value='$average'></td>
[/code]


I have tried calling the form element by a different name in case it was confusing it with the javascript variable but this made no difference.

Thanks again
Copy linkTweet thisAlerts:
@smercerMar 09.2005 — [i]Originally posted by cowjl [/i]

[B]Hi,



yes you are right i am very much a beginnner at this ?



Can anyone enlighten me more as to what is wrong with that bit of code? As far as I can tell it sets the javascript variable to the value of the php variable. It appears to work as I use the javascript variable to populate the input boxes. This seems to work ok, it is the passing of the php variable in the following line of code that only works when the check box is not ticked:

[code=php]
<td><input type='hidden' name='average' value='$average'></td>
[/code]


I have tried calling the form element by a different name in case it was confusing it with the javascript variable but this made no difference.

Thanks again [/B][/QUOTE]


What about the people that do not have JavaScript enabled on their machine? How are they going to use you site if it is dependant on JavaScript? Have you got any plans for these type of visitors?

Having it like the way you do is not only annoying for the visitor, but could set you back as well. Example: the user clicks a tick box clicks submit and because the user has not enabled JavaScript, It does not get entered into your database. time goes by and all the while the visitor is expecting something from you, but does not happen because he does not realise that it did not go into your database, the visitor sends you an unpleasant email, stating this that and the other. But it could be much worse depending on what it is, for instance loss of property (in the case of insurance) or even life or death(I am properly exaggerating here), But the point I am trying to get across is that repercussions such as legal issues could come up, but I hope you get my meaning.
Copy linkTweet thisAlerts:
@cowjlauthorMar 09.2005 — What about the people that do not have JavaScript enabled on their machine? How are they going to use you site if it is dependant on JavaScript? Have you got any plans for these type of visitors?

Having it like the way you do is not only annoying for the visitor, but could set you back as well. Example: the user clicks a tick box clicks submit and because the user has not enabled JavaScript, It does not get entered into your database. time goes by and all the while the visitor is expecting something from you, but does not happen because he does not realise that it did not go into your database, the visitor sends you an unpleasant email, stating this that and the other. But it could be much worse depending on what it is, for instance loss of property (in the case of insurance) or even life or death(I am properly exaggerating here), But the point I am trying to get across is that repercussions such as legal issues could come up, but I hope you get my meaning.[/quote]


Hi,

Yes I have considered those that don't have javascript enabled. The form still works without the JavaScript. If they don't check the box, they fill in values and the validation is done with PHP once the form is submitted. If they do check the box, and click submit they won't be able to see the values entered into the box on the original page, but they will be displayed back to them on the results page (i have tested this with javascript disabled on my browser).

Very good points though...thanks for bringing them up ?
Copy linkTweet thisAlerts:
@smercerMar 09.2005 — Well now that you know, I think it time we go to the next step, Here is a tutoral on passing varibles via the cookie

http://www.htmlgoodies.com/beyond/javascript/article.php/3471111

Hope that helps
Copy linkTweet thisAlerts:
@cowjlauthorMar 09.2005 — Well now that you know, I think it time we go to the next step, Here is a tutoral on passing varibles via the cookie

http://www.htmlgoodies.com/beyond/j...cle.php/3471111

Hope that helps[/quote]


Hi smercer,

Thanks for that - I will give it a go now ?
×

Success!

Help @cowjl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...