0

Alright I apologize for the completely uninformative title but i had no idea what to call it. Basically I have an array with multiple values and I only want information to be sent from certain forms if a certain value is met in the array.

<?php
if(isset($_POST['cb1'])) {
$cb1 = implode(",", $_POST['cb1']);   
} else {
$cb1 = "";
}
?>

<?php
if(isset($_POST['cb2'])) {
$cb2 = implode(",", $_POST['cb2']);   
} else {
$cb2 = "";
}
?>

<?php
if(isset($_POST['tutor'])) {
$tutor = implode(",", $_POST['tutor']);   
} else {
$tutor = "";
}
?>









<?php


$con = mysqli_connect("localhost","root","admin","form_info");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
/*mysql_connect("localhost","root","admin")or die("Cannot Connect");
mysql_select_db("form_info")or die("Cannot connect to database name");// connect to database db_name*/



/*Get Values from form*/

$todays_date="";
 if(isset($_POST['todays_date'])){ $todays_date = $_POST['todays_date']; }
$student_fname="";
 if(isset($_POST['student_fname'])){ $student_fname = $_POST['student_fname']; }
$student_lname="";
 if(isset($_POST['student_lname'])){ $student_lname = $_POST['student_lname']; }
$student_id="";
 if(isset($_POST['student_id'])){ $student_id = $_POST['student_id']; }
$contact_number="";
 if(isset($_POST['contact_number'])){ $contact_number = $_POST['contact_number'];}
$student_email="";
 if(isset($_POST['student_email'])){ $student_email = $_POST['student_email']; }
$tr_date="";
 if(isset($_POST['tr_date'])){ $tr_date = $_POST['tr_date']; }
$start_time="";
 if(isset($_POST['start_time'])){ $start_time = $_POST['start_time']; }
$finish_time="";
 if(isset($_POST['finish_time'])){ $finish_time = $_POST['finish_time']; }
$course_num1="";
 if(isset($_POST['course_num1'])){ $course_num1 = $_POST['course_num1']; }
$course_num2="";
 if(isset($_POST['course_num2'])){ $course_num2 = $_POST['course_num2']; }
$course_num3="";
 if(isset($_POST['course_num3'])){ $course_num3 = $_POST['course_num3']; }
$section_num1="";
 if(isset($_POST['section_num1'])){ $section_num1 = $_POST['section_num1']; }
$section_num2="";
 if(isset($_POST['section_num2'])){ $section_num2 = $_POST['section_num2']; }
$section_num3="";
 if(isset($_POST['section_num3'])){ $section_num3 = $_POST['section_num3']; }









    /*
    if(isset($_POST['cb2'])&&$_POST['cb2']="Need Thoery Room") {
mysqli_query($con,"INSERT INTO student_info2 (todays_date, student_fname, student_lname, student_id, contact_number, student_email, cb1, cb2, tr_date, start_time, finish_time, tutor)  VALUES( '$todays_date','$student_fname','$student_lname', '$student_id','$contact_number','$student_email','$cb1','$cb2','$tr_date','$start_time','$finish_time','$tutor')")
;  
}
*/

    if($cb2="Need Tutoring"){
mysqli_query($con,"INSERT INTO student_info2 (todays_date, student_fname, student_lname, student_id, contact_number, student_email, cb1, cb2, tutor, course_num1, course_num2, course_num3, section_num1, section_num2, section_num3)   VALUES( '$todays_date','$student_fname','$student_lname', '$student_id','$contact_number','$student_email','$cb1','$cb2','$tutor','$course_num1','$course_num2','$course_num3','$section_num1','$section_num2','$section_num3')")
;



    }


error_reporting(E_ALL ^ E_NOTICE);  


    if($_POST['cb2']!="Need Thoery Room" and $_POST['cb2']!="Need Tutoring")    {           
mysqli_query($con,"INSERT INTO student_info2 (todays_date, student_fname, student_lname, student_id, contact_number, student_email, cb1, cb2, tutor)    VALUES( '$todays_date','$student_fname','$student_lname', '$student_id','$contact_number','$student_email','$cb1','$cb2','$tutor')")
;
    }




    ?>

So this is my php and below is my html form. As you can see I tried to use an if statement but to be truthful I just don't know php very well yet and I feel like I'm missing something completely. To be specific as to what I'm trying to do: I want to only send the data in tr_date, start_time, and finish_time if the theory room checkbox is checked. I'm also plan on doing the same thing to the table data for when tutoring is checked.

<form action="info.php" method="post">

<b>Today's Date</b><input name="todays_date" type="text" id="todays_date"><br />
<b>Student Name:</b> 
First:<input name="student_fname" type="text" id="student_fname">
Last:<input name="student_lname" type="text" id="student_lname"><br />
<b>Student ID:</b><input name="student_id" type="text" id="student_id"><br />
<b>Contact Number:</b><input name="contact_number" type="text" id="contact_number"><br />
<b>Email:</b><input name="student_email" type="text" id="student_email"><br />

<h2>I resquest help with my (Check all that apply):</h2>

<input name="cb1[]" type="checkbox" value="DreamSpark" />DreamSpark<br />
<input name="cb1[]" type="checkbox" value="Downloading Books"/>Downloading Books<br />
<input name="cb1[]" type="checkbox" value="VM Ware"/>VM Ware<br />
<input name="cb1[]" type="checkbox" value="Virtual Library"/>Virtual Library<br />
<input name="cb1[]" type="checkbox" value="Downloading Software"/>Downloading Software<br />
<input name="cb1[]" type="checkbox" value="Printing"/>Printing<br />


<h2>I request to speak with a student worker or Rob about (Check all that apply):</h2>

<input name="cb2[]" type="checkbox" value="Student Network Account Not Working" />Student Network Account Not Working<br />
<input name="cb2[]" type="checkbox" value="Student Email Not Working"/>Student Email Not Working<br />
<input name="cb2[]" type="checkbox" value="Student Portal Not Working"/>Student Portal Not Working<br />
<input name="cb2[]" type="checkbox" value="Student Wireless Not Working"/>Student Wireless Not Working<br />
<input name="cb2[]" type="checkbox" value="Need Online Class Help/ E-book reader"/>Need Online Class Help/ E-book reader<br />
<input name="cb2[]" type="checkbox" value="Need Theory Room"/>Need Theory Room for Capstone/Group Project<br />

Date:<input name="tr_date" type="text" id="tr_date">
Start Time:<input name="start_time" type="text" id="start_time">
Finish Time:<input name="finish_time" type="text" id="finish_time">
<br />

<input name="cb2[]" type="checkbox" value="Need Tutoring"/>Need Tutoring (Select tutor and Fill out "Course(s) for tutoring Schedule" Below)<br />

<input name="tutor[]" type="checkbox" value="DeMartino"/>DeMartino
<input name="tutor[]" type="checkbox" value="Kritzik"/>Kritzik
<input name="tutor[]" type="checkbox" value="Peisl"/>Peisl
<input name="tutor[]" type="checkbox" value="Hall"/>Hall
<br />
<br />


<table border="1" style="width:300px">


<tr>
<th colspan=2 >Course(s) for tutoring Schedule</th>
</tr>
<tr>
<th>Course Number</th>
<th>Section Number</th>
</tr>

<tr>
<td><input name="course_num1" type="text" id="course_num1"></td>
<td><input name="section_num1" type="text" id="section_num1"></td>
</tr>
<tr>
<td><input name="course_num2" type="text" id="course_num2"></td>
<td><input name="section_num2" type="text" id="section_num2"></td>
</tr>
<tr>
<td><input name="course_num3" type="text" id="course_num3"></td>
<td><input name="section_num3" type="text" id="section_num3"></td>
</tr>







</table>
2
Contributors
5
Replies
49
Views
3 Years
Discussion Span
Last Post by world_weapon
0

Looking over this and reading the description of the issue, I am unclear on a couple of points. When you say you only want the select values from a certain form, it's not clear to me which direction you are trying to send this. I only see one form open tag, which leads me to beleive that there is just one form.

If I am not mistaken, all the (input) values are going to be sent when the form is submitted. The default values will be set for any input values that weren't set/modified. If you are trying to get only the tr_date start and finish time, then just reference those values in the conditional block checking for the presence of the Need Theory value.

Maybe I'm missing something in your problem/issue though. When someone fills that form though, the presence of those form elements within the POST form will cause the request to include those values with any request in the headers. If you don't want those post values to be sent at all ( I guess to make checks for their existence fail ) you can either make your form dynamic ( with jquery/javascript ) to only add the elements once the theory box is checked, or just live with the overhead that the data will take up with the POST request when the form is submitted. If I misunderstood the problem, just clarify it like you are talking to a 5 year old. I'm a bit slow sometimes. :D

0

No actually I think you helped quite a bit haha. What I'm looking for is closer to the dynamic form description you gave. I just want it so that if someone decides to input information into the tr_date, start_time, and finish_time fields without checking the need theory room box, the information won't be inserted into the database. Just so there isn't un-needed information filling the database. It's more of a prevention thing more than an actual problem

0

If it's about insertion into the database, if it's mysql, you can just omit the column names and data from the INSERT statements. Of course, if those columns have a default value in their definition, they will be taking up space regardless so you will want to keep that in mind and I am not 100% sure about all, but I know some database engines may reserve that memory in the MySQL data anyway. The conditional block for changing the INSERT query would then be based on cb2 and the compare for theory string.

0

I've thought of a new approach to the situation. How would I make it so that the user could not enter information into the date, start time, and finish time boxes without checking the need theory room box. In other words could I grey out those fields until the box is checked, then have those boxes become available to the user when it is checked?

0

If you have jquery support on the page, you can keep those elements hidden and render or dynamically generate them on the event of checking the need theory box. If they uncheck it, you can either remove the element from the dom, or re-hide them. I suppose you could use bare javascript if you don't have jquery as well.

Jquery Append
Jquery Remove

Edited by world_weapon: References to jquery

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.