Click to See Complete Forum and Search --> : how to NOT send a variable unless something is entered in the field
marcnyc
06-27-2009, 09:31 PM
Hello, I am working with a form which has a lot of text fields which submits some data via POST. I would like some of this data to ONLY be sent if the user actually has entered something in the textfield, and if the user didn't I would like that variable not to even be sent via POST (not even an empty value)... Is this possible somehow?
Thanks
bradgrafelman
06-27-2009, 10:57 PM
Well, since PHP operates on the server-side of things, you're going to have to look for a client-side solution (e.g. Javascript).
Before we dive into the Javascript route, though, could you explain to us what's wrong with an empty field being POST'ed? Why not just detect that it's empty and ignore it?
marcnyc
06-28-2009, 01:45 AM
To answer your question and make a long story short. I wrote a script who sends (via POST) a large amount of data (about 600 lines of text). I just installed this script on another server but here it doesn't send any data (after submitting the form you get to the next page but $_POST is empty)... My first thought was that php_post_max was set smaller on the other server but I checked and they are the same... Since I can't think of any other reason why it would work on one server and not the other I decided to go the route of making those 600 lines smaller/less (I actually troubleshooted the script and if I sent 300 lines or so it does go through fine). A lot of the text I am sending I don't need unless a variable is set to a certain value, so if I can determine whether the variable is empty or not I can considerably diminish the amount of text sent and make this script work on any server... Make sense?
bradgrafelman
06-28-2009, 02:07 AM
In that case, can you clarify a bit further what "text fields" you're trying to apply this filtering to? Is it just <input type="text"> elements?
EDIT: Assuming so, and knowing that disabled form elements aren't included in the POST'ed data, here's what I came up with:
<script type="text/javascript">
function disableEmpty(frm) {
nodeList = frm.getElementsByTagName('input');
for(var i=0; i < nodeList.length; i++)
if(nodeList[i].type == "text" && nodeList[i].value.length == 0)
nodeList[i].disabled = true;
}
</script>
<!-- and to apply this to the form: -->
<form method="post" action="foobar.php" onsubmit="disableEmpty(this)">
marcnyc
06-28-2009, 03:04 AM
Yes they are all input type="text" elements.
I tried your code but it doesn't seem to work. All empty fields are still sent. Any idea why?
bradgrafelman
06-28-2009, 01:15 PM
If by "empty" you mean they are completely empty (e.g. a length of 0; not even a single space), then no, I don't know why. I tested it on my local computer using Firefox, Google Chrome, and IE6 - all three only sent fields that had data typed into them.
I know you said that it's a form with "a lot" of fields, but is there any way you could either post some example code you tried, or else give us a URL to the form you have in place now? If you don't feel comfortable sharing the URL with the world, feel free to send it to me in a private message and I would be happy to take a look at it.
marcnyc
06-29-2009, 01:24 AM
I don't have a problem posting the code, I just thought it might confuse people rather than enlighten them.
<form method="post" name="menuedit">
<?php
// include data files
$filename = 'menu.data.inc.php';
$filepath = $tbs_datafolder.'/menu/'.$l.'/'.$filename;
if ( !file_exists($filepath) )
$filepath = $filepath.'.bak';
include($filepath);
include('includes/menu.array.inc.php');
?>
<script type="text/javascript">
<!--
function me_img_swap(element_ID,img_ID) {
if ( document.images[img_ID].src == 'http://<?=$_SERVER['SERVER_NAME'];?>/images/plus.gif' ) {
document.images[img_ID].src = 'http://<?=$_SERVER['SERVER_NAME'];?>/images/minus.gif';
document.getElementById(element_ID).style.display = 'block';
} else {
document.images[img_ID].src = 'http://<?=$_SERVER['SERVER_NAME'];?>/images/plus.gif';
document.getElementById(element_ID).style.display = 'none';
}
}
function me_showHide(element_ID,img_ID) {
if (document.getElementById(element_ID).style.display == 'none') {
document.getElementById(element_ID).style.display = 'block';
me_img_swap(element_ID,img_ID);
} else {
document.getElementById(element_ID).style.display = 'none';
me_img_swap(element_ID,img_ID);
}
}
//-->
</script>
<?php
if ( !isset($_POST['page']) || ( isset($_POST['page']) && $_POST['page'] != 'editmenu' ) ) {
echo $tbs_lng['mchinstr'].'<br /><br /><br />'.$rn.$rn;
$input_width = 10;
$tbs_link_keys = array_keys($tbs_link);
$li = 0;
echo '<div id="menu_editing">'.$rn;
$menu = array_keys($menus);
for ( $mi=0; $mi<$mimax; $mi++ ) {
echo ' <div name="me_m'.$mi.'" id="me_m'.$mi.'" style="color:#FFFFFF;background-color:#000000;display:block"> <a href="#" onClick="me_showHide(\'me_s'.$mi.'\',\'me_img_m'.$mi.'\');"><img src="images/plus.gif" border="0" name="me_img_m'.$mi.'" id="me_img_m'.$mi.'" /></a> <b>Menu '.( $mi < 9 ? ' ' : '' ).($mi + 1).'</b> <input size="'.$input_width.'" type="text" name="M[]" value="'.( !empty($_POST['M'][$mi]) ? $_POST['M'][$mi] : stripslashes($menu[$mi]) ).'" /> '.$tbs_lng['fllnk'].pages( $tbs_link_keys[$li], $menu[$mi] ).'<br />'.$rn; $li++;
$sub = array_keys($menus[$menu[$mi]]);
echo ' <div name="me_s'.$mi.'" id="me_s'.$mi.'" style="margin-left:20px;color:#FFFFFF;background-color:#666666;display:none">'.$rn;
for ( $si=0; $si<$simax; $si++ ) {
echo ' <a href="#" onClick="me_showHide(\'me_m'.$mi.'_ss'.$si.'\',\'me_img_m'.$mi.'_s'.$si.'\');"><img src="images/plus.gif" border="0" name="me_img_m'.$mi.'_s'.$si.'" id="me_img_m'.$mi.'_s'.$si.'" /></a> <b>Sub-Menu '.( $si < 9 ? ' ' : '' ).($si + 1).'</b>'.$tbs_lng['of'].'Menu '.($mi + 1).' <input size="'.$input_width.'" type="text" name="M'.$mi.'S[]" value="'.( !empty($_POST['M'.$mi.'S'][$si]) ? $_POST['M'.$mi.'S'][$si] : stripslashes($sub[$si]) ).'" /> '.$tbs_lng['fllnk'].pages( $tbs_link_keys[$li], $sub[$si] ).'<br />'.$rn; $li++;
echo ' <div name="me_m'.$mi.'_ss'.$si.'" id="me_m'.$mi.'_ss'.$si.'" style="margin-left:20px;color:#FFFFFF;background-color:#999999;display:none">'.$rn;
for ( $ssi=0; $ssi<$ssimax; $ssi++ ) {
echo ' <b>Sub-Sub-Menu '.( $ssi < 9 ? ' ' : '' ).($ssi + 1).'</b>'.$tbs_lng['of'].'Sub-Menu '.($si + 1).$tbs_lng['of'].'Menu '.($mi + 1).' <input size="'.$input_width.'" type="text" name="M'.$mi.'S'.$si.'S[]" value="'.( !empty($_POST['M'.$mi.'S'.$si.'S'][$ssi]) ? $_POST['M'.$mi.'S'.$si.'S'][$ssi] : stripslashes($menus[$menu[$mi]][$sub[$si]][$ssi]) ).'" /> '.$tbs_lng['fllnk'].pages( $tbs_link_keys[$li], $menus[$menu[$mi]][$sub[$si]][$ssi] ).'<br />'.$rn; $li++;
}
echo ' </div>'.$rn;
}
echo ' </div>'.$rn;
echo ' </div>'.$rn;
}
echo $rn.'<input type="hidden" name="page" value="editmenu" />'.$rn.'<br /><br /><input type="submit" value="'.$tbs_lng['mchbutton'].'" />'.$rn;
echo '</div>'.$rn;
}
// if the menu has been changed, write the data files!!!
else if ( isset($_POST['page']) && $_POST['page'] == 'editmenu' ) {
// external data file content
$data = '<?'.$rn;
for ( $mi=0; $mi<$mimax; $mi++ ) {
$data .= ' $m'.$mi.' = "'.$_POST['M'][$mi].'"; /* links to */ $tbs_link["m'.$mi.'"] = "'.$_POST['filelink'][str_replace(' ','_',$_POST['M'][$mi])].'";'.$rn;
for ( $si=0; $si<$simax; $si++ ) {
$data .= ' $m'.$mi.'s'.$si.' = "'.$_POST['M'.$mi.'S'][$si].'"; /* links to */ $tbs_link["m'.$mi.'s'.$si.'"] = "'.$_POST['filelink'][str_replace(' ','_',$_POST['M'.$mi.'S'][$si])].'";'.$rn;
for ( $ssi=0; $ssi<$ssimax; $ssi++ ) {
$data .= ' $m'.$mi.'s'.$si.'ss'.$ssi.' = "'.$_POST['M'.$mi.'S'.$si.'S'][$ssi].'"; /* links to */ $tbs_link["m'.$mi.'s'.$si.'ss'.$ssi.'"] = "'.$_POST['filelink'][str_replace(' ','_',$_POST['M'.$mi.'S'.$si.'S'][$ssi])].'";'.$rn;
}
}
}
$data .= '?>'.$rn;
// create external data file
$file = fopen($filepath, 'w+') or die("ERROR: Can't create/edit file. Check permissions!");
fwrite($file, $data);
fclose($file);
// create external data file backup
$file = fopen($filepath.'.bak', 'w+') or die("ERROR: Can't create/edit backup file. Check permissions!");
fwrite($file, $data);
fclose($file);
echo '<span class="approved">'.$tbs_lng['mchok'].'</span>';
}
?>
</form>
The depth of this menu structure is defined in an external file.
depth = 3 would mean that there are 3 menus and for every menu 3 sub-menus and for every one of those 3 sub-menus another 3 sub-sub-menus each... in other words depth=3 would mean 3+9+27 menus = 40 lines or input text fields being sent.
I noticed that if I have menu depth set to 4 or less $_POST contains what I expect and the script works, but if menu depth is > 4 then $_POST is empty and since that condition isn't met (nothing is sent at all) the $data variable isn't being populated and the file with the new menu isn't being written.
The strange thing is that on one server this script works as expected and on another server it doesn't work...
I hope the code helps and doesn't add confusion
marcnyc
06-29-2009, 03:16 AM
Hey bradgrafelman, I wanted to let you know that I found a seemingly unrelated bug in the code (something to do with headers) and after fixing it to my amazement this part of the script seemed to work as well (I guess messed up headers were preventing $_POST to work) so you don't have to worry about this issue any longer!
Thanks for looking into it.
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.