ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This function here is supposed to fill up $mchecked with values from checkboxes that are marked as checked .... problem is that $mchecked gets all values from the checkboxes, including the unchecked ones...
Code:
function poll_mlist(){
<?
$query="SELECT maillist from maillisthead";
$data=pg_exec($connect,$query);
$rows=pg_numrows($data);
if($rows>0){
for($i=0; $i < $rows; $i++){
$result=pg_result($data, $i, "maillist");
//The name of each checkbox is fetched from the database
?>
if(document.frm_asti_email.<?=$result?>.checked=
=true){
<?
$mchecked[] = $result; //add the value to array if 'true'
?>
alert("<?=$result?> is checked"); //just for testing
}
<?
}
}
foreach($mchecked as $key => $value){
echo "$value\n"; //just for testing
}
?>
}
You are mixing up javascript and PHP, and what they each do. Here is the process:
1. Browser asks server for PHP page
2. Server executes PHP and sends all output to the browser
3. Browser parses HTML and executes javascript
So, look at this piece of code, with my additions:
Code:
(PHP)<?
(PHP) for($i=0; $i < $rows; $i++){
(PHP) $result=pg_result($data, $i, "maillist");
(PHP)?>
(JS) if(document.frm_asti_email.<?=$result?>.checked==true){
(PHP) <?
(PHP) $mchecked[] = $result; //add the value to array if 'true'
(PHP) ?>
(JS) alert("<?=$result?> is checked"); //just for testing
Can you see what will happen? When the PHP runs, it knows nothing about javascript's "if" statement - it just adds that JS code to the output, and the "$mchecked[] = $result" line will be run every time throughout the for loop. PHP runs through the loop and:
1. sets $result
2. outputs the if() line (with the value of $result)
3. adds $result to the $mchecked array
4. outputs the alert command.
Is there a reason you're trying to do so much with Javascript? Processing form input is what PHP does, not to mention how unreliable Javascript is from browser to browser. Just submit the form and have your PHP code check the $_POST variable. Sort of like this:
Hmmm... ah I get it now. PHP isn't aware of that if statement so the variable now fetches all of it. thanks for clearing that up.
The code I'm debugging right now is littered with both php & javascript, it's isn't really my code and I'm not that familiar with both languages. So I guess somehow I'm forced to use javascript 'cause there are some variables here that are javascript ... I can't really convert it to php cause I just learned that both are different (client/server side).
When you submit a checkbox, the PHP var is either set to nothing (checkbox turned off) or the 'value' you set in the HTML - so instead of testing against "on" (the default), you should maybe test against $result:
after seeing all the unchecked values again I suspected something, I checked the source from my browser and I got this:
Code:
<html>
<head>
<script language="javascript">
function poll_mlist(){
alert("mchecked: Array");
}
getting warm, I have a feeling the reason I didn't get any results using 'on' is because php fetched the results too soon before the user could click on submit() and check the checkboxes.
I should transfer the function somewhere ... I'm beginning to hate javascript
(this is not 100% accurate, but it is the basic concept - the important point is that the browser sends the data to PHP when you say so, PHP does not come and get the data from your computer)
When the webserver starts up PHP to process the page, it passes those two variables through in the $_POST variable (i.e $_POST["username"] and $_POST["password"]). There is another variable called $_GET, if you use method="get" on your form instead of method="post", so this is one thing you could check.
For more debugging, when you use PHP to print an array as if it were a string, it just prints "Array", which is why your alert statement is probably not showing you what you want to see. Try
Code:
print "Mchecked is: "
print_r( $mchecked );
print "POST array is: "
print_r( $_POST );
print "GET array is: "
print_r( $_GET );
These will probably help you to see what is going wrong, and where your checkbox values are.
Thanks for explaining to me ... and those print_r's were very useful.
Re: empty $mchecked:
Still nothing, if( $_POST[$result] = "on/off/$result") doesn't give me any results. The only time it gave the right values correctly is when I used the if(document.frm_asti_email.<?=$result?>.checked==true) condition ... but It'll be tricky going back there again cause I need to pass the javascript array results to a php one and as I'm told, that's just not possible.
Did $_POST or $_GET contain the stuff you wanted? Did you remove the quotes when you tested the values against $result? I assure you, PHP is built to do this, as long as everything is built correctly (i.e the checkboxes are contained within the form that is being submitted). Javascript is actually counter productive if you want the server to know which boxes are selected.
strangely no, $_POST & $_GET didn't contain any values, and yes I did remove the quotes.
Here's how I analyzed how the entire page works:
<php> includes
<js> contains functions, checks for blanks
<js> if all is ok, document.form.submit()
<php> if save == 1? get all values & insert
<php> call script for checking checkboxes
<html> form, action="test.php?save=1" , post
<php> generate checkboxes
<html> submit button, go to functions
The start of the script has the includes, then javascript functions for checking the forms for human error. Php will then check if $save==1, if not continue with the script ... Checkboxes are generated using a different script and then finally the submit button.
If the submit button is checked, the form will let save=1. The page refreshes with a save=1 in the address, then it'll check if save==1 again, if it is then the script will now insert the details on the database. This is also where the checkboxes come in, all checked ones will have their values saved.
Sounds like you are either using register_globals, or an older version of PHP (or both). register_globals is a security issue, and the $_POST and $_GET variables were introduced to counteract this.
Try changing the if section again, but this time to:
The thing is, if register_globals is being used, form-posted parameters are stored directly in variables, not in the $_POST array. That is, if you have a textbox called "chuck", your script will receive the contents in a variable called $chuck.
I would like to recommend turning off register_globals, but it's very likely the whole site has been written to take advantage of this old behaviour.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.