I am trying to get the response of a survey, all question answers are displayed in a while loop and in action PHP page I am not getting proper responses, I'm getting the right response for the radio button only, I'm attaching the code.
<?php
$i = 1;
while ($row = mysqli_fetch_array($questions)) {
?>
<div class="control-group">
<label class="control-label" for="focusedInput">(<?php echo $i; ?>)
<?php
$questionid = $row['question_id'];
$question = $row['question'];
?>
<input type="hidden" name="questionid" value="<?php echo $questionid; ?>" />
<input type="hidden" name="question" value="<?php echo $question; ?>" />
<?php echo $row['question']; ?></label>
<div class="controls">
<?php
if ($row['answer_type'] == "Ratings") {
echo "<p>
Low<input type='radio' name='rating$i' value='1' id='rating_0'>
<input type='radio' name='rating$i' value='2' id='rating_1'>
<input type='radio' name='rating$i' value='3' id='rating_2'>
<input type='radio' name='rating$i' value='4' id='rating_3'>
<input type='radio' name='rating$i' value='5' id='rating_4'>High
</p>";
} else if ($row['answer_type'] == "Comments") {
echo "<textarea name='answer' cols='' rows=''></textarea>";
}
$i++;
echo "<br />";
?>
</div>
</div>
<?php } ?>
Action file code:
foreach($_POST as $val){
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$_SESSION[surveyid],$_POST[questionid],'$_POST[question]',$val,'$_POST[answer]')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
I want to insert the survey answers in the MySQL table including the fields displayed in the output picture.
Your form tags are missing from the exposed code, so there might be some extra fields available that are important for the scenario.
There is no obvious reason for using foreach on $_POST. You should explain further why you're cycling it.
Here is some code for your action file, that might work for you:
/* create a prepared statement */
$stmt = $mysqli->stmt_init();
if ($stmt->prepare("INSERT INTO review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) VALUES(1,?,?,?,?,?)")) {
/* bind parameters for markers */
$stmt->bind_param("sssss", $_SESSION['surveyid'], $_POST['questionid'], $_POST['question'], $val, $_POST['answer']);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
Related
I am creating a form and in the form, all the input fields will display dynamically. I am able to create the text, email, password, checkbox but not able to create the select and radio. I have to use the select tag for country and radio for gender.
I am using CodeIgniter.
<?php
$example_from_name = array('name' => 'user_from_view'); //assigning form name
echo form_open('formbuilder_control/example_from_view',$example_from_name);
foreach ($data as $key) {
$exp_fields_name=$key->fields_name; //here I am getting the name of field name
$exp_fields_type=$key->fields_type; //here I am getting the input type
?>
<div class="form-group row label-capitals">
<label class="col-sm-5 col-form-label"><?php echo $exp_fields_name;?></label>
<div class="col-sm-7">
<input type="<?php echo $exp_fields_type;?>" name="<?php echo $exp_fields_name;?>" placeholder="<?php echo $exp_fields_name;?>" class="form-control" />
<?php echo form_error($exp_fields_name); ?>
</div>
</div>
<?php
}
?>
<div class="form-buttons-w btn_strip">
<input type="submit" value="Save" class="btn btn-primary margin-10">
</div>
<?php echo form_close(); ?>
You don't have enough dynamic data for some of the fields. You will have to pass more from your database. A few of the fields will require values (select, radio, checkbox) Here is the basic structure (as the others were trying to say):
<?php
$example_from_name = array('name' => 'user_from_view'); //assigning form name
echo form_open('formbuilder_control/example_from_view',$example_from_name);
foreach ($data as $key) {
$exp_fields_name=$key->fields_name; //here I am getting the name of field name
$exp_fields_type=$key->fields_type; //here I am getting the input type
echo "<div class=\"form-group row label-capitals\">";
echo "<label class=\"col-sm-5 col-form-label\">$exp_fields_name</label>";
echo "<div class=\"col-sm-7\">";
if(in_array($exp_fields_type,['text','email','password'])){
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" class=\"form-control\" />";
}elseif($exp_fields_type=='checkbox'){
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" value=\"[something needed here]\" class=\"form-control\" /> $exp_fields_name";
}elseif($exp_fields_type=='select'){
echo "<select name=\"$exp_fields_name\">";
echo "<option></option>"; // you will have to determine a way to populate the options
// ... more options
echo "</select>";
}elseif($exp_fields_type=='radio'){
echo "$exp_fields_name <input type=\"$exp_fields_type\" name=\"$exp_fields_name\" value=\"[something needed here]\" class=\"form-control\" />";
}else{
echo "Whoops, uncaught field type!";
}
echo form_error($exp_fields_name);
echo "</div>";
echo "</div>";
}
echo "<div class=\"form-buttons-w btn_strip\">";
echo "<input type=\"submit\" value=\"Save\" class=\"btn btn-primary margin-10\">";
echo "</div>";
echo form_close();
We, volunteers, cannot help you with the missing components of your design. You will need to figure out how you are going to pass the necessary options/values to the necessary html form elements.
If you have known field names, you might try writing the conditionals using $exp_fields_name instead of $exp_fields_type to cover a few exceptional cases. Perhaps exchange the above if-block with the following:
if($exp_fields_name=='gender'){
echo "<input type=\"radio\" name=\"gender\" value=\"male\" class=\"form-control\" /> Male<br>";
echo "<input type=\"radio\" name=\"gender\" value=\"female\" class=\"form-control\" /> Female";
}elseif($exp_fields_name=='country'){
echo "<select name=\"country\" class=\"form-control\" />";
echo "<option>India</option>";
echo "<option>Sri Lanka</option>";
echo "<option>Japan</option>";
// ...continue as needed
echo "</select>";
}else{ // all other types default to input tag
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" class=\"form-control\" />";
}
I assume "checkbox" inputs will have multiple inputs, so you may need to write a custom check for that group as well.
you have to use IF or switch using this type:
<?php
if($exp_fields_type == 'text'){
$input = '<input type="text" name="'.$exp_fields_name;.'" value="" placeholder="" />';
}else if($exp_fields_type == 'radio'){
$input = '<input type="radio" name="'.$exp_fields_name;.'" value="" placeholder="" />';
}
?>
Your code will works only for input tags. So you should check the type before creating the tag
for example :
<?php
if($exp_fields_type == 'select'){
$input = '<select name="'.$exp_fields_name.'" placeholder="" >
<option value="">option 1 </option>
</select>';
}else if($exp_fields_type == 'textarea'){
$input = '<textarea name="'.$exp_fields_name.'"></textarea>';
}else {
$input = '<input type="'.$exp_fields_type.'" name="'.$exp_fields_name.'" value="" placeholder="" />';
}
?>
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a>  ";
echo "<input type='submit' name='acc' value='Accept'>   <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a>  ";
echo "<input type='submit' name='acc' value='Accept'>   <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo
I am fetching the data from the MySQL table in a while loop, and inserting the form data into another MySQL table in action page in foreach, but I do not get the correct value of the radio button, I am attaching my code, please help.
<?php
$i = 1;
$j = 1;
while ($row = mysqli_fetch_array($questions)) {
?>
<div class="control-group">
<label class="control-label" for="focusedInput">(<?php echo $i; ?>)
<?php
$questionid = $row['question_id'];
$question = $row['question'];
?>
<input type="hidden" name="questionid[]" value="<?php echo $questionid; ?>" />
<input type="hidden" name="question[]" value="<?php echo $question; ?>" />
<?php echo $row['question']; ?></label>
<div class="controls">
<?php
if ($row['answer_type'] == "Ratings") {
echo "
<p>
Low<input type='radio' name='rating$i' value='1' id='rating_0'>
<input type='radio' name='rating$i' value='2' id='rating_1'>
<input type='radio' name='rating$i' value='3' id='rating_2'>
<input type='radio' name='rating$i' value='4' id='rating_3'>
<input type='radio' name='rating$i' value='5' id='rating_4'>High
</p>
";
$i++;
} else if ($row['answer_type'] == "Comments") {
echo "<textarea name='answer[]' cols='' rows=''></textarea>";
$j++;
}
echo "<br />";
?>
</div>
</div>
<?php } ?>
Action File Code
foreach($_POST['questionid'] as $key=>$questionid){
$questionid = $_POST['questionid'][$key];
$answer = $_POST['answer'][$key];
$result3 = mysqli_query($con, "select question,answer_type from questions where question_id=$questionid;");
while($row = mysqli_fetch_array($result3)) {
$question = $row['question'];
$answer_type = $row['answer_type'];
if($answer_type == "Comments") {
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_freeresponse) values(1,$_SESSION[surveyid],$questionid,'$question','$answer')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
else if($answer_type == "Ratings") {
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating) values(1,$_SESSION[surveyid],$questionid,'$question',$key)";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
}
$i++;
}
Output
I want to store the ratings displayed as a radio button, I guess its taking the counter incremented as variable $key, I don't know how to store the values of the radio button.
I guess you will get your radio button value as,
$ratingKey = "rating".$key;
$rating = $_POST[$ratingKey];
Than use $rating in your insert query instead of $key.
INSERT into review_details (review_id,survey_id,question_id,question,answer_rating)
values(1,$_SESSION[surveyid],$questionid,'$question',$rating)
i m trying to increment value 1 using while loop condition, i m developing quiz generation system, but stuck here and not found any solution, please help and tell what to do....
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$i++;
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech1; ?>'><?php echo $qech1."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech2; ?>'><?php echo $qech2."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech3; ?>'><?php echo $qech3."<br>"; ?><br>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $qech4; ?>'><?php echo $qech4."<br>"; ?><br>
<?php
}
?>
you seen above code, it is $i = 1; and then i echo $i as well in 4 fields of radio. it retrives around 3 questions from database and each question has 4 values, but all of the total of 12 values of 3 questions has same name that is 1, it is not increment using $i++ not sure why ?
here is out put
<span style='font-size:18px;font-weight:550px; '>english</span><br><input type='radio' name='1' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<input type='radio' name='2' value='jkj'>jkj<br><br>
<input type='radio' name='2' value='kjk'>kjk<br><br>
<span style='font-size:18px;font-weight:550px; '>adnan </span><br><input type='radio' name='1' value='jhwjksahajsh'>jhwjksahajsh<br><br>
<input type='radio' name='2' value='kjhjhjh'>kjhjhjh<br><br>
<input type='radio' name='2' value='kjhkjjh'>kjhkjjh<br><br>
<input type='radio' name='2' value='hjkjhj'>hjkjhj<br><br>
<span style='font-size:18px;font-weight:550px; '>sdfsd</span><br><input type='radio' name='1' value='gfgf'>gfgf<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gfg'>gfg<br><br>
<input type='radio' name='2' value='gf'>gf<br><br>
You can use a Heredoc string to help you with this. I made a simple example that will output what you want in a simple way. Take a look:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$i = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$qech1=$rows['optiona'];
$qech2=$rows['optionb'];
$qech3=$rows['optionc'];
$qech4=$rows['optiond'];
$correct=$rows['correct'];
echo <<<HTML
<input type='radio' name='$i' value='$qech1'>$qech1<br><br>
<input type='radio' name='$i' value='$qech2'>$qech2<br><br>
<input type='radio' name='$i' value='$qech3'>$qech3<br><br>
<input type='radio' name='$i' value='$qech4'>$qech4<br><br>
HTML;
$i++;
}
The reason it isn't incrementing is because you are calling $i right after incrementing it.
while ... {
$i++
...
..name="$i"...
}
Right now, it goes through the loop once and prints all names as 2. The next time all of them would be 3, etc.
What you want is more of a foreach loop.
I assigned basic values just for my testing purposes. I personally would eliminate assigning $quech1 = $rows[optiona]. You can just work with the array of $rows with the foreach loop.
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$correct=$rows['correct'];
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' name='<?php echo $i; ?>' value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
You'll see from my inspect element, that you are now getting incremented names:
However, if I'm not mistaken. Don't you generally want all of the names to be the same for a group of radio buttons? You'll want unique ID's. And then have all buttons that are to be grouped together with the same name. This makes it so only one button in that group can be selected. If that is what you're trying to accomplish, use the revised code:
<?php
$ques2=mysql_query("SELECT * FROM questions WHERE course='english' AND id='$id'");
$name = 1;
while($rows=mysql_fetch_assoc($ques2))
{
$rows['optiona'] = "optiona";
$rows['optionb'] = "optionb";
$rows['optionc'] = "optionc";
$rows['optiond'] = "optiond";
$name++;
}
$i = 0;
foreach ($rows as $row){
$i++;?>
<input type='radio' id='<?php echo $i; ?>' name='<?php echo $name; ?>'value='<?php echo $row; ?>'><?php echo $row."<br>"; ?>
<?php } ?>
Only change is that $i is being assigned to id now. And we'll declare a $name counter as well. We'll increment that in the while loop, so that it will only be incremented once for each set of button options. And then you use $name in the name attribute for the button.
That's what makes sense to me. But without more information, we don't really know exactly what you're trying to accomplish. :)
I'm trying to grasp OOP and I decided to build a site that accesses a sql database.
It's been working out great so far I have several tables, but I've run into a snag
I access the database and create an object array with
$Dog_array = $sth->fetchAll(PDO::FETCH_CLASS, 'Dog');
Simplified but my class looks like this.
class Dog {
private $name;
function get_name {
return $this->name;
}
list that build my table
function edit_table($Dog) {
echo "<table><tr><form action='update.php' method ='post'>";
echo "<input type='hidden' name='id' value='" . $id ."'>";
echo "<td>". $Dog->get_id() ."</td>";
?>
<td><input type='text' name='rname' value="<?php echo $Dog->get_rname(); ?>"></td>
<td><input type='text' name='cname' value="<?php echo $Dog->get_cname(); ?>"></td>
<td><input type='text' name='dob' value="<?php echo $Dog->get_dob(); ?>"></td>
<td><input type='radio' name='gender' value='male' <?PHP if($Dog->get_gender() == 'male'){ echo "checked=\"checked\""; } ?> /> Male
<input type='radio' name='gender' value='female' <?PHP if($Dog->get_gender() == 'female'){ echo "checked=\"checked\""; } ?> />Female</td>
<td><input type='text' name='sire' value="<?PHP echo "builddropdown" ?> "></td>
<td><input type='text' name='dam' value="<?PHP drop_menu() ?>"></td>
<?php
if ($Dog->get_available()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
echo "<td><input type=\"checkbox\"" . $checked . "name=\"available\" value=\"TRUE\"/></td>";
if ($Dog->get_display()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
}
echo "<td><input type=\"checkbox\"" . $checked . "name=\"display\" value=\"TRUE\"/></td></tr></table>";
?>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<?php
}
?>
<input type='submit' value='update'/></form>
<FORM METHOD="LINK" ACTION="index.php">
<INPUT TYPE="submit" VALUE="Back">
</FORM>
<?php
}
?>
My drop down
function drop_menu() {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?></option>
<?php } ?>
</select>
</form>
<? }
My drop down menu creates a list of all of the dogs.
I get the error Invalid argument supplied for foreach
I get why I get the error I just don't know how to clean this up so I don't have to pass my Object array into my edit_table function so I can pass it into drop down function
.
I thought about creating a child class but the menu is built from an array of the objects not one object so I don't know how to make this work either. New to forum posting, so feel free to recommend some corrections in how I ask for help.
It's a problem with the arrage $Dog_array scope.
You need to pass the $Dog_array to the drop_menu function.
<?php
function drop_menu($Dog_array) {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?>></option>
<?php } ?>
</select>
</form>
<? }
And whenever you need to call the function, pass the array normally.
Also you can pass by reference
<?php
function drop_menu(&$Dog_array) {?>
In this case any manipulation to the array inside the function will impact the original array directly.
function dropdown( $name, array $options, $selected=null )
{
//array_push($options, "");
/*** begin the select ***/
//<select name="dropdownarray" id="dropdownarray">
$dropdown = '<select name="'.$name.'" id="'.$name.'">';
$selected = $selected;
/*** loop over the options ***/
echo "<b>$name : </b>"; // Title
$dropdown .= '<option value=></option>'."\n"; //empty data
foreach( $options as $key=>$option )
{
/*** assign a selected value ***/
$select = $selected==$key ? ' selected' : null;
/*** add each option to the dropdown ***/
$dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
/*** close the select ***/
$dropdown .= '</select>'."\n";
/*** and return the completed dropdown ***/
return $dropdown;
}
enter code here