Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling checkbox values from a file.

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I have a php page where a user can set information requested and upon submit it writes the information to a file. On my form I have two check boxes. If I check both of them and click submit, the information is put into the file, but when the page reads in the file, I cannot get the first checkbox ti display it's check mark. It should be performing a checked=checked if a certain value is true. The bottom one works fine as you will see in my code, they are basically the same. And it doesn't matter if it is both checked or just the first one checked it still wont do what it needs to do.

configsys.php:
Code:
<?php

if($_POST['submit'])
{
//Will write the data collected for the configuration to a file

$ip_addr = '172.16.254.3';
$gateway = '172.16.254.1';
$netmask = '255.255.255.0';
$etreby = $_POST['ivr'];
$ivrport = $_POST['ivrport'];
$vm_ext = '198';
$down = '196';
$lang = $_POST['lang'];
$rsync1 = $_POST['rsync1'];
$rsync2 = $_POST['rsync2'];
$rsync3 = $_POST['rsync3'];
$rsync4 = $_POST['rsync4'];
if(!isset($_POST['rsyncme']))
  {
   $check = "off";
  }else{
   $check = $_POST['rsyncme'];
  }

if(!isset($_POST['vpn']))
  {
   $vpn = "off";
  }else{
   $vpn = "vpntomsi";
  }

$file = "/etc/asterisk/ivrconfig.conf";
$command = "/var/[URL unfurl="true"]www/ivrconfig/ivrconfig";[/URL]
$opfile = fopen($file, 'w') or die('Cannot open file');
$stringdata = $ip_addr . "\n" . $netmask . "\n" . $gateway . "\n" . $etreby . "\n" . $ivrport . "\n" . $vm_ext . "\n" . $down . "\n" . $lang . "\n" . $rsync1 . "\n" . $rsync2 . "\n" . $rsync3 . "\n" . $rsync4 . "\n" . $check . "\n" . $vpn;
fwrite($opfile, $stringdata);
fclose($opfile);

}

$file1 = file("/etc/asterisk/ivrconfig.conf") or die('Cannot open ivrconfig.conf');
?>
<html>
 <head>
  <title>Werks Configuration</title>
 </head>
<form action="configsys.php" method="post">

<?php
require('header.php');
?>
<p></p>
 <table bgcolor="grey" align="center">
  <tr>
   <th align="center"><font align="center">Enter the information you wish to assign to this device</font></th>
  </tr>
  <tr>
   <td>IVR Server IP:</td>
   <td><input type="text" name="ivr" value="<? echo $file1[3] ?>"></td>
  </tr>
  <tr>
   <td>IVR Server Port:</td>
   <td><input type="text" name="ivrport" value="<? echo $file1[4] ?>"></td>
  </tr>
  <tr>
   <td>M for Multilingual or E for English:</td>
   <td><input type="text" name="lang" value="<? echo $file1[7] ?>"></td>
  </tr>
  <tr>
   <td>IP Address of resync pc:</td>
   <td><input type="text" name="rsync1" value="<? echo $file1[8] ?>"></td>
  </tr>
  <tr>
   <td>Rsync username:</td>
   <td><input type="text" name="rsync2" value="<? echo $file1[9] ?>"></td>
  </tr>
  <tr>
   <td>Rsync password:</td>
   <td><input type="text" name="rsync3" value="<? echo $file1[10] ?>"></td>
  </tr>
  <tr>
   <td>Rsync virtual directory:</td>
   <td><input type="text" name="rsync4" value="<? echo $file1[11] ?>"></td>
  </tr>
  <tr>
   <td><input type='checkbox' name='rsyncme' <?php if($file1[12] == "on"){echo 'checked="checked"';} ?>> Enable Rsync Backup</td>
  </tr>
  <tr>
   <td><input type='checkbox' name='vpn' <?php if($file1[13] == "vpntomsi"){echo 'checked="checked"';} ?>> Enable VPN to MSI</td>
  </tr>
  <tr>
   <td><input type='submit' name='submit' value='Submit'></td>
  </tr>
 </table>
</form>
<?
// End Config Display
?>
</html>

ivrconfig.conf file:
Code:
172.16.254.3
255.255.255.0
172.16.254.1
63.237.119.48
7004
198
196
M
192.168.1.1
bill
12345
phonesync
on
vpntomsi
 
Check the value being returned by the $file1[12] variable.

Its definitely different to 'on' so there is likely an invisible character being returned. Check the length of the string, and make sure there are no extraneous spaces, or line breaks in there.

Try using trim on your lines to clean them of strange characters.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
the file() function splits each line including the new line character into an array. so $file[11] will be "on\n" (or \r\n or \r)

instead use
Code:
$file1 = file("/etc/asterisk/ivrconfig.conf", [red]FILE_IGNORE_NEW_LINES[/red]) or die('Cannot open ivrconfig.conf');
 
by doing so you are adding unnecessary server overhead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top