I have here a page with iframe. Inside the iframe, it will display all the questions in multiple choice. The problem is that I don't know how to get all the values of the checked radio buttons inside the iframe. The button is in the parent page (I haven't included it yet).
This is the screen capture of my page (questions.php):
This is my iframe code from questions.php:
<div class="question-display test">
<iframe src="questions/frame.php" height="500px" width="100%">
</iframe>
</div>
This is my frame.php (displaying all the questions)
<?php
include('config.php');
$view_questions=mysql_query("SELECT * FROM questions ORDER BY RAND()");
$count=mysql_num_rows($view_questions);
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="questions-contents text-size test" style="margin-left: 10px;">
<p align="justify"><strong><?php echo $i;?>.)</strong> <?php echo $row['QUESTION'];?></p>
</div>
<div class="answer-indent" style="margin-left: 40px; margin-bottom: 20px;">
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="1" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_1'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="2" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_2'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="3" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_3'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="4" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_4'];?></class="color">
</label>
<label class="radio" style="display: none;" >
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="5" data-toggle="radio" checked>
</label>
</div>
<?php
$i++; }
?>
Before I make my frame.php inserted to iframe from the questions.php page, I have a php codes for checking the answers of the users (the checked radio buttons).
$questions = mysql_query("SELECT * FROM questions");
while($row=mysql_fetch_assoc($questions)){
if($_POST[$row['QUESTION_NO']]==$row["ANSWER"])
{
$score++;
}
elseif($_POST[$row['QUESTION_NO']]==5)
{
$unanswered++;
}
else
{
$wrong_score++;
}
}
Please help me, I'm doing this since yesterday but I didn't get it. I don't know how to do it.
You can give id to your iframe and access its elements.
document.getElementById('frameid').contentWindow.document.getElementById('abc')
Where abc is your radio button element.
Then you can do whatever you want to do with element.
But seriously, you can do the same thing in Div also with proper css.
I think you should give same name to all the checkboxes. And you can allot the question no. for their id
So the form will be
<div class="answer-indent" style="margin-left: 40px; margin-bottom: 20px;">
<label class="radio">
<input type="radio" name="question" id="<?php echo $row['QUESTION_NO'];?>" value="1" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_1'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="question" id="<?php echo $row['QUESTION_NO'];?>" value="2" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_2'];?></class="color">
</label>
.
.
.
.
</div>
So we can get the checked box values using simple snippet.
showing below
<?php
$values = $_POST['question'];
// optional
foreach ($values as $selected){
echo $selected."<br />";
}
?>
Please try this
the iframe form action should be the iframe parent url
<form action="parenturl.php" method="POST">
When you dont add the action attribute to your form element the default action will
be submitting the form to the current page by GET method, so when you trying to
view the post data in the parent there is nothing to show because the data was posted to the
iframe itself and not to the parent.
so make sure you define the method and and action.
Related
I have tried in many ways to fix it but without success.
I'm trying to sort categories through the checkbox but it does not work. The post is made public during editing but the checkbox is not active and after the post update is checkbox removes the id and the post loses from the category.
<form name="addpost" method="post" enctype="multipart/form-data">
<div class="form-group m-b-20">
<label for="exampleInputEmail1">Category</label>
<br>
<?php
$ret=mysqli_query($con,"select id,CategoryName from tblcategory where Is_Active=1");
while($result=mysqli_fetch_array($ret))
{
?>
<input type="checkbox" name="category" id="category" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
<?php } ?>
<button type="submit" name="update" class="waves-effect waves-light pull-right butoni">Update </button>
</div>
</form>
This:
<input type="checkbox" name="category" id="category" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
is placed in a loop... so there are more than one of these yes?!
We can't use the same id, and name for multiple inputs of a form or in html ... instead :
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br>
this will make things appear in one after the other...
if you want the box to be checked add an if in the loop
if($result['something'] should be checked){ ?>
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?> checked "><?php echo htmlentities($result['CategoryName']);?><br><?php
} else {
<input type="checkbox" name="category[]" id="category<?php echo $result['id'];?>" value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?><br><?php
}
just fix the:
if($result['something'] should be checked){rest of the code}
to be correct according the when checkbox should be checked and when not.
For checkbox to be checked we add a checked in the input line.
So I am using a form multiple radio buttons, The purpose is for the user to rate different parts of the site with 10 questions.
The user presses "start rating" button and the 1st question appears, then the 2nd etc.
When he finishes all of the 10 questions a submit button appears.
The problem is that, when I'm trying to retrieve these values on PHP, I get nothing, its like the radio buttons hasn't been pressed at all.
Newbie in HTML.
HTML
<form name="f1" action="rate.php" method="POST">
<!--1η ερώτηση-->
<div class="question_div" id="question_div1">
<center><p class="questiontitle"><b>Ερώτηση 1 από 10</b></p></center>
<p>Νομίζω ότι θα ήθελα να χρησιμοποιώ αυτά τα παιχνίδια συχνά.</p>
<div class="answer_div">
<div class="answer_options">
<label class="opt_label">
<input type="radio" name="input1" id="input1_1" value="1" class="option">
<span class="opt_span"><b>1</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_2" value="2" class="option">
<span class="opt_span"><b>2</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_3" value="3" class="option">
<span class="opt_span"><b>3</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_4" value="4" class="option">
<span class="opt_span"><b>4</b></span>
</label>
<label class="opt_label">
<input type="radio" name="input1" id="input1_5" value="5" class="option">
<span class="opt_span"><b>5</b></span>
</label>
</div>
<div class="answer-options">
<div align="left" style="width:20%;
float:left;
margin-right:30px;">Διαφωνώ απολύτως</div>
<div align="right" style="width:20%;
float:right;
margin-left:30px;">Συμφωνώ απολύτως</div>
</div>
</div>
<button class="nextbtn" type="button" id="btn1" onclick="(function(){
document.getElementById('question_div1').style.display = 'none';
document.getElementById('question_div2').style.display = 'block';
return false;
})();return false;">Επόμενη Ερώτηση</button>
</div>
this is the start of the form and the 1st question, the other questions follow the same architecture.
PHP:
if(!empty($_POST["f1"])){
$q1 = $_POST['input1'];
$q2 = $_POST['input2'];
$q3 = $_POST['input3'];
$q4 = $_POST['input4'];
$q5 = $_POST['input5'];
$q6 = $_POST['input6'];
$q7 = $_POST['input7'];
$q8 = $_POST['input8'];
$q9 = $_POST['input9'];
$q10 = $_POST['input10'];
}
It's because you have the same <input type="radio" name="input1" id="input1_1" value="1" class="option"> with the same name value. Try removing the name a attribute in your <form> and do one radio first and in your php file do this:
HTML
<form method="POST" action='rate.php'>
<div> question </div>
<input type="radio" name="input1">
<input type="submit">
</form>
rate.php
<?php
if(isset($_POST['input1']) {
echo $_POST['input1'];
//do your code here
}
This is my code of radio button i want image radio buttton is alredy check but it's not working
<div class="col-md-4" style="padding-top: 24px;" >
<label>Visual</label>
<div>
<label class="radio-inline">
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off"><?php echo __('Image') ?>
</label>
<label>or</label>
<label class="radio-inline">
<input value="video" type="radio" name="media_option[]" class="media_option" autocomplete="off" ><?php echo __('Video') ?>
</label>
</div>
Try this.
Don't set radio name as array and not need to set autocomplete attribute.
<div class="col-md-4" style="padding-top: 24px;" >
<label>Visual</label>
<div>
<label class="radio-inline">
<input type="radio" value="image" name="media_option" class="media_option" checked="checked" ><?php echo __('Image') ?>
</label>
<label>or</label>
<label class="radio-inline">
<input value="video" type="radio" name="media_option" class="media_option" ><?php echo __('Video') ?>
</label>
</div>
Its working fine, check here:
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off">One
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off">Two
when you are using radio, only one radio can be selected within one group (having same name). The same is working here.
How about this?
This will always work
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off">
If not try this
<input checked type="radio" value="image" name="media_option[]" class="media_option" autocomplete="off">
Just change the checked="checked" to checked only :
Here is the code ,please try:
<div class="col-md-4" style="padding-top: 24px;" >
<label>Visual</label>
<div>
<label class="radio-inline">
<input type="radio" value="image" checked name="media_option[]" class="media_option" autocomplete="off"><?php echo __('Image') ?>
</label>
<label>or</label>
<label class="radio-inline">
<input value="video" type="radio" name="media_option[]" class="media_option" autocomplete="off" ><?php echo __('Video') ?>
</label>
/div>
I came here because I had the same issue and without seeing the rest of the code in the question, won't make any assumptions.
However, if the code above wasn't within a form element, that could have caused the issue.
That was the issue in my case, after placing the radio elements within the form, the checked attribute worked again as expected. Hopefully this helps someone else out as well.
I have a check box simple form with two fileds, on submit I am submitting the values yes or no in the database, now in edit I want to show that checkbox selected.
<div class="form-group col-md-6">
<label> Is Paid</label></br>
Yes <input type="checkbox"
name="paid"
value="yes"
value=" <?php echo $row_cat['is_paid']; ?>"
/>
No <input type='checkbox' name="paid" value="no" />
</div>
How can I show prefilled checkbox on edit page ?
Try
<div class="form-group col-md-6">
<label> Is Paid</label></br>
Yes <input type="checkbox"
name="paid"
value="yes"
<?php echo $row_cat['is_paid']=='yes'?' checked="checked" ':''; ?>
/>
No <input type='checkbox'
name="paid"
value="no"
<?php echo $row_cat['is_paid']=='no'?' checked="checked" ':''; ?>
/>
</div>
I have a database with pictures that i can delete from or use them in different parts of the the website by choosing one of the radio buttons in the popup.Problem is everytime I click I click on a picture the name of the first picture in the database comes up not the one from the picture i clicked . What's wrong? I used PHP 5.3 and HeidiSQL
<?php
// ...
$result=mysql_query("SELECT * FROM imagini WHERE menu_id=6");
while($data=mysql_fetch_row($result)){ ?>
<div class="tag">
<div id='container_poze_originale'>
Alege alt rol
<div id="my_popup" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
close
<form action="popup.php" method="post" >
<input type="text" name="alt-rol" value="<?php echo $data[1];?>" /> //always returns the name of the first picture in the database not the one i select
<input type="submit" value="Adauga imagine" class="buton_imagine" />
<div class="radio">
<input type="radio" name="tip_imagine" value="0"/><label for="tip_imagine" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1"/><label for="tip_imagine" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2"/><label for="tip_imagine" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3"/><label for="tip_imagine" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4"/><label for="tip_imagine" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5"/><label for="tip_imagine" class="radio2">Background</label>
</div>
</form>
</div>
<div class="imagine_originala">
<img src= "../upload/original/<?php echo $data[1];?>" /></a>
</div>
<div class="Btag" style="display:none;">
<div id="buton_slide4" >
Sterge</td>
</div>
</div>
</div>
</div>
<?php
}
?>
I think I understand your question. You are clicking on
Alege alt rol
and you are surprised that the same <div> always appears?
Well, it is simply because an id should be unique. And if you cycle through your images you create a new <div> each time with the same id: my_popup. When clicking on the link (no matter which one), JavaScript will then just take the first <div> it finds with the id my_popup. So you should just assign an unique id to each div so your JavaScript knows which one to open. You could do this by appending your image id to the div id (assuming you have a unique image id in $data[1]).
So change it to (shortened):
<? while($data=mysql_fetch_row($result)) { ?>
<div class="tag">
<div id="container_poze_originale">
Alege alt rol
<div id="my_popup<?=$data[1]?>" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
...
</div>
</div>
</div>
<? } ?>
Oh and it might be good to put all those huge inline CSS attributes into an external stylesheet, so it will be easier to maintain/change your webpage later on. And it makes things a bit more readable... :-)
I think your form is ok when you click on the radio but not when you click on the label.
The for attribute of the label need an ID as reference like this :
<input type="radio" name="tip_imagine" value="0" id="tip0" /><label for="tip0" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1" id="tip1"/><label for="tip1" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2" id="tip2"/><label for="tip2" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3" id="tip3"/><label for="tip3" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4" id="tip4"/><label for="tip4" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5" id="tip5"/><label for="tip5" class="radio2">Background</label>
With your actual code, the FOR is always the same so you get always the first.