Page 1 of 1

needing some help with a simple saving function

Posted: Wed Oct 27, 2010 1:36 am
by crazykeff
hey guys! im rather new with php, so im needing some help with my codings. like mentioned, i have some issues with saving my radio buttons into text file. :banghead: and the validations for the buttons have issues too.

can someone plz point out my mistakes and guide me along?

Code: Select all

<?php
$gender = $_POST["gender"];
$age = $_POST["age"];
$suptype = $_POST["suptype"];
$knowledge = $_POST["knowledge"];
$overall = $_POST["overall"];
$speed = $_POST["speed"];
$repknowledge = $_POST["repknowledge"];
$result = $_POST["result"];
$recommend = $_POST["recommend"];
$personality = $_POST["personality"];
$addinfo = $_POST["addinfo"];
?>
<?php 
session_start(); 
?>
<html>
<head>
  <title>My Online Survey</title>
</head>
<body>
<table width="100%" border="1" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <th width="190" height="23" scope="col"><p>
      <?php
 echo $_SESSION["username"]
 ?>
    </p>
    <p><?php 
date_default_timezone_set ("Asia/Singapore"); 
echo date("d/m/y : H:i:s", time())
?></p></th>
    <th width="275" scope="col"><a href='mcq.php'>MCQ</a></th>
    <th width="271" scope="col"><a href='Feedback.php'>FeedBacks</a>
      </p></th>
    <th width="223" scope="col"><a href="logout.php">Log out</a></th>
  </tr>
</table>
<form action="survey.php" method="post">
<table border="1">
  <tr>
    <td width="526">What gender are you?</td>
    <td colspan="5"><input type="radio" name="gender" value="Male">
Male |
  <input type="radio" name="gender" value="Female">
Female</td>
  </tr>
  <tr>
    <td>What is your age group?</td>
    <td colspan="5"><input type="radio" name="age" value="0_8" />
      0 - 8 |
      <input type="radio" name="age" value="9_16" />
      9 - 16 |
      <input type="radio" name="age" value="17_24" />
      17 - 24 <br />
      <input type="radio" name="age" value="25 - 32" />
      25 - 32 |
      <input type="radio" name="age" value=" 33 - 40" />
      33 - 40 |
      <input type="radio" name="age" value="41_x" />
      Older than 41</td>
  </tr>
  <tr>
    <td>Was this phone or email support?</td>
    <td colspan="5"><input type="radio" name="suptype" value="email" />
      Email |
      <input type="radio" name="suptype" value="phone" />
      Phone</td>
  </tr>
  <tr>
    <td height="31">What is your knowledge with computers?</td>
    <td colspan="5"><input type="radio" name="knowledge" value="little" />
      Little |
      <input type="radio" name="knowledge" value="some" />
      Some |
      <input type="radio" name="knowledge" value="lots" />
      A Lot |
      <input type="radio" name="knowledge" value="expert" />
      Expert</td>
  </tr>
  <tr>
    <td colspan="6" bgcolor="#cccccc"></td>
  </tr>
  <tr>
    <td></td>
    <th width="103">Very Dissatisfied</th>
    <th width="79">Dissatisfied</th>
    <th width="58">Satisfied</th>
    <th width="91">Very Satisfied</th>
    <th width="79">Neutral</th>
  </tr>
  <tr>
    <td>Overall how satisfied are you with the support you received?</td>
    <td><input type="radio" name="overall" value="0" /></td>
    <td><input type="radio" name="overall" value="1" /></td>
    <td><input type="radio" name="overall" value="2" /></td>
    <td><input type="radio" name="overall" value="3" /></td>
    <td><input type="radio" name="overall" value="4" /></td>
  </tr>
  <tr>
    <td>How satisfied are you with the speed of the support?</td>
    <td><input type="radio" name="speed" value="0" /></td>
    <td><input type="radio" name="speed" value="1" /></td>
    <td><input type="radio" name="speed" value="2" /></td>
    <td><input type="radio" name="speed" value="3" /></td>
    <td><input type="radio" name="speed" value="4" /></td>
  </tr>
  <tr>
    <td>How would you rate the knowledge of the support representative?</td>
    <td><input type="radio" name="repknowledge" value="0" /></td>
    <td><input type="radio" name="repknowledge" value="1" /></td>
    <td><input type="radio" name="repknowledge" value="2" /></td>
    <td><input type="radio" name="repknowledge" value="3" /></td>
    <td><input type="radio" name="repknowledge" value="4" /></td>
  </tr>
  <tr>
    <td>How satisfied were you with the result of the support?</td>
    <td><input type="radio" name="result" value="0" /></td>
    <td><input type="radio" name="result" value="1" /></td>
    <td><input type="radio" name="result" value="2" /></td>
    <td><input type="radio" name="result" value="3" /></td>
    <td><input type="radio" name="result" value="4" /></td>
  </tr>
  <tr>
    <td>Would you recommend us to a friend or coworker for support requests in the future?</td>
    <td colspan="3"><input type="radio" name="recommend" value="yes" />
      Yes </td>
    <td colspan="2"><input type="radio" name="recommend" value="no" />
      No</td>
  </tr>
  <tr>
    <td>How would you rate the personability of the support representative?</td>
    <td><input type="radio" name="personability" value="0" />
      0 - Not Very</td>
    <td><input type="radio" name="personability" value="1" />
      1</td>
    <td><input type="radio" name="personability" value="2" />
      2</td>
    <td><input type="radio" name="personability" value="3" />
      3</td>
    <td><input type="radio" name="personability" value="4" />
      4 - Very</td>
  </tr>
</table>

<table width="982" border="1">
  <tr>
    <td width="975" height="61">Any additional information?<br>
      <textarea name="addinfo" id="txt" cols="80" rows="5"></textarea>
      <br>

    </label></td>
  </tr>
</table>
<br />
  <input type="submit" value="submit" name="submit" />
</form>
</body>
</html>

<?php 
if (isset ($_POST['submit'])) { 
$problem = FALSE; 
if (empty ($_POST['gender'])) { 
$problem = TRUE; 
print "Please select your gender!"; 
} 
if (empty ($_POST['age'])) { 

  print "Please select your age!" ;
exit;
} 
if (empty ($_POST['suptype'])) { 
 
print "Please select your support type."; 
exit;
} 
if (empty ($_POST['knowledge'])) { 
 
print "Please select your level of knowledge."; 
exit;
} 
if (empty ($_POST['overall'])) { 
 
print "Please select your overall level of satisfaction."; 
exit;
} 
if (empty ($_POST['speed'])) { 
 
print "Please select your level of satisfaction with support speed."; 
exit;
} 
if (empty ($_POST['repknowledge'])) { 
 
print "Please select a rating for the level of knowledge of our support representative."; 
exit;
} 
if (empty ($_POST['result'])) { 
 
print "Please select your level of satisfaction with the result of support."; 
exit;
}
if (empty ($_POST['recommend'])) { 
 
print "Please select whether will you recommend our support services."; 
exit;
}  
if (empty ($_POST['personality'])) { 
 
print "Please select a rating for the level of personality of our support representative."; 
exit;
}    
// if there is no problem open the file and write to it
   $fp=fopen('User_Survey.txt', 'ab' ); 

$data =($_POST['gender'])."	".($_POST['age'])."	".($_POST['suptype'])."	".($_POST['knowledge'])."	".($_POST['overall'])."	".($_POST['speed'])."	".($_POST['repknowledge'])."	".($_POST['result'])."	".($_POST['recommend'])."	".($_POST['personality'])."\r\n"; 
// Write data and close file! 
fwrite ( $fp, $username); 
fwrite ($fp, $data);
fwrite ($fp, '$addinfo');
fclose ( $fp ); 
print "Thank you for your submission!"; 
}
exit;
?>

Re: needing some help with a simple saving function

Posted: Wed Oct 27, 2010 6:24 am
by phppro62
Hi mate
You missed some Variables and Arguments . I tried my best to make it working
Now you can write to the file and everything is working well

Code: Select all

<?php
   
session_start();
if (isset($_POST["submit"]))  {                            
$gender = $_POST["gender"];
$age = $_POST["age"];
$suptype = $_POST["suptype"];
$knowledge = $_POST["knowledge"];
$overall = $_POST["overall"];
$speed = $_POST["speed"];
$repknowledge = $_POST["repknowledge"];
$result = $_POST["result"];
$recommend = $_POST["recommend"];
$personality = $_POST["personality"];
$addinfo = $_POST["addinfo"];
}
  showSurvey();
  
if (isset ($_POST['submit'])) {
$problem = FALSE;
$filename=("survey2.txt") ;
$fd = fopen($filename, "w+")
or die("Can’t open file $filename");
 $data=fwrite($fd,"$gender,$age,$suptype,$knowledge,$overall,$speed,$repknowledge,$result,$recommend,$personality,$addinfo") ;
fclose($fd);
if (empty ($_POST['gender'])) {
$problem = TRUE;
echo "Please select your gender!";
exit;
}
if (empty ($_POST['age'])) {

  echo "Please select your age!" ;
exit;
}
if (empty ($_POST['suptype'])) {
 
echo "Please select your support type.";
exit;
}
if (empty ($_POST['knowledge'])) {
 
echo "Please select your level of knowledge.";
exit;
}
if (empty ($_POST['overall'])) {
 
echo "Please select your overall level of satisfaction.";
exit;
}
if (empty ($_POST['speed'])) {
 
echo "Please select your level of satisfaction with support speed.";
exit;
}
if (empty ($_POST['repknowledge'])) {
 
echo "Please select a rating for the level of knowledge of our support representative.";
exit;
}
if (empty ($_POST['result'])) {
 
echo "Please select your level of satisfaction with the result of support.";
exit;
}
 if (empty ($_POST['recommend'])) {
 
echo "Please select Would you recommend us to a friend or coworker for support requests in the future?.";
exit;
} 
if (empty ($_POST['personality'])) {
 
echo "Please select a rating for the level of personality of our support representative.";
exit;
} 
}   

?>
<?
 function showSurvey(){  
?>
<html>
<head>
  <title>My Online Survey</title>
</head>
<body>
<table width="100%" border="1" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <th width="190" height="23" scope="col"><p>
      <?php
 echo $_SESSION["username"]
 ?>
    </p>
    <p><?php
date_default_timezone_set ("Asia/Singapore");
echo date("d/m/y : H:i:s", time())
?></p></th>
    <th width="275" scope="col"><a href='mcq.php'>MCQ</a></th>
    <th width="271" scope="col"><a href='Feedback.php'>FeedBacks</a>
      </p></th>
    <th width="223" scope="col"><a href="logout.php">Log out</a></th>
  </tr>
</table>
<form action="survey2.php" method="post">
<table border="1">
  <tr>
    <td width="526">What gender are you?</td>
    <td colspan="5"><input type="radio" name="gender" value="Male">
Male |
  <input type="radio" name="gender" value="Female">
Female</td>
  </tr>
  <tr>
    <td>What is your age group?</td>
    <td colspan="5"><input type="radio" name="age" value="0_8" />
      0 - 8 |
      <input type="radio" name="age" value="9_16" />
      9 - 16 |
      <input type="radio" name="age" value="17_24" />
      17 - 24 <br />
      <input type="radio" name="age" value="25 - 32" />
      25 - 32 |
      <input type="radio" name="age" value=" 33 - 40" />
      33 - 40 |
      <input type="radio" name="age" value="41_x" />
      Older than 41</td>
  </tr>
  <tr>
    <td>Was this phone or email support?</td>
    <td colspan="5"><input type="radio" name="suptype" value="email" />
      Email |
      <input type="radio" name="suptype" value="phone" />
      Phone</td>
  </tr>
  <tr>
    <td height="31">What is your knowledge with computers?</td>
    <td colspan="5"><input type="radio" name="knowledge" value="little" />
      Little |
      <input type="radio" name="knowledge" value="some" />
      Some |
      <input type="radio" name="knowledge" value="lots" />
      A Lot |
      <input type="radio" name="knowledge" value="expert" />
      Expert</td>
  </tr>
  <tr>
    <td colspan="6" bgcolor="#cccccc"></td>
  </tr>
  <tr>
    <td></td>
    <th width="103">Very Dissatisfied</th>
    <th width="79">Dissatisfied</th>
    <th width="58">Satisfied</th>
    <th width="91">Very Satisfied</th>
    <th width="79">Neutral</th>
  </tr>
  <tr>
    <td>Overall how satisfied are you with the support you received?</td>
    <td><input type="radio" name="overall" value="0" /></td>
    <td><input type="radio" name="overall" value="1" /></td>
    <td><input type="radio" name="overall" value="2" /></td>
    <td><input type="radio" name="overall" value="3" /></td>
    <td><input type="radio" name="overall" value="4" /></td>
  </tr>
  <tr>
    <td>How satisfied are you with the speed of the support?</td>
    <td><input type="radio" name="speed" value="0" /></td>
    <td><input type="radio" name="speed" value="1" /></td>
    <td><input type="radio" name="speed" value="2" /></td>
    <td><input type="radio" name="speed" value="3" /></td>
    <td><input type="radio" name="speed" value="4" /></td>
  </tr>
  <tr>
    <td>How would you rate the knowledge of the support representative?</td>
    <td><input type="radio" name="repknowledge" value="0" /></td>
    <td><input type="radio" name="repknowledge" value="1" /></td>
    <td><input type="radio" name="repknowledge" value="2" /></td>
    <td><input type="radio" name="repknowledge" value="3" /></td>
    <td><input type="radio" name="repknowledge" value="4" /></td>
  </tr>
  <tr>
    <td>How satisfied were you with the result of the support?</td>
    <td><input type="radio" name="result" value="0" /></td>
    <td><input type="radio" name="result" value="1" /></td>
    <td><input type="radio" name="result" value="2" /></td>
    <td><input type="radio" name="result" value="3" /></td>
    <td><input type="radio" name="result" value="4" /></td>
  </tr>
  <tr>
    <td>Would you recommend us to a friend or coworker for support requests in the future?</td>
    <td colspan="3"><input type="radio" name="recommend" value="yes" />
      Yes </td>
    <td colspan="2"><input type="radio" name="recommend" value="no" />
      No</td>
  </tr>
  <tr>
    <td>How would you rate the personaility of the support representative?</td>
    <td><input type="radio" name="personaility" value="0" />
      0 - Not Very</td>
    <td><input type="radio" name="personaility" value="1" />
      1</td>
    <td><input type="radio" name="personaility" value="2" />
      2</td>
    <td><input type="radio" name="personaility" value="3" />
      3</td>
    <td><input type="radio" name="personaility" value="4" />
      4 - Very</td>
  </tr>
</table>

<table width="982" border="1">
  <tr>
    <td width="975" height="61">Any additional information?<br>
      <textarea name="addinfo" id="txt" cols="80" rows="5"></textarea>
      <br>

    </label></td>
  </tr>
</table>
<br />
  <input type="submit" value="submit" name="submit" />
</form>
</body>
</html>

<?}?>




Re: needing some help with a simple saving function

Posted: Wed Oct 27, 2010 8:58 am
by zainmirza
I thing you should have built all Form optional Components in select html form tag that is represented as <select name="any"><option value="1">Male</option>
<option value="2">Female</option>
</select>

This approach reduce the number of error

Re: needing some help with a simple saving function

Posted: Wed Oct 27, 2010 9:24 am
by crazykeff
a big thx to phppro62! thx alot for the great help :D
and thx to zainmirza for the tip! i'll be sure to adopt ur suggestion! :)