I created a form and I want multiple radio questions in my form with the same output (Yes,No ).
Here is my script:
<?php
echo '<tr>
<script>
function displayResult(check)
{
document.getElementById("result").value=check
}
</script>
<td width="60%">'
.$row1['vraag'].'
</td>
<td>
<input type="radio" name="check" onclick="displayResult(this.value)" value="ja">Ja
<input type="radio" name="check" onclick="displayResult(this.value)" value="nee">Nee
</td>
<td>
<input type="hidden" name="vragen'.$aantalBranches.'[]" value="'.$row1['vraag'].'"/>
<input id="result" type="text" name="antwoorden'.$aantalBranches.'[]"/>
</td>
</tr>';
As you can see I sent the question with it (vragen) and the answer (antwoorden) when the form is filled.
The problem I have now is when I select the second question also with a radio the output of the first one also changes.
Anyone can help me in the right direction with radio button ?
Thanks in advance
All radio buttons with the same name are treated as being in the same group - and only one item from the group can be selected at the same time. So, your second set of radio boxes need their name set to a different group to the name used by the first.
Related
I am building a online exam application, here paper name and no. of papers are retrieved from database. Now I want to get the paper code of that paper for which I clicked the start button. Code for the table is here:
<form method="post" action="exam_page.php" >
<table >
<tr style="background-color: #7F859E;color:white; height:50px;">
<th style="padding-left:140px; width:550px;">Paper</th>
<th style="padding-left:40px;">Time</th>
<th style="padding-left:40px;">Duration</th>
<th style="padding-left:40px; width:250px;"></th>
</tr>
<?php
$i=1;
while($row=mysql_fetch_array($rs)){?>
<tr style="height:80px; background-color: #CCCCCC;">
<td style="padding-left:40px;">
<input type="text" value="<?=$row['paper_code']?>" name="paper_code<?=$i?>" readonly><?=$row['paper_name']?>
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['time']?>" readonly style="width:90px;">
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['duration']?> Min" readonly style="width:90px;">
</td>
<td style="padding-left:40px;"><button style="width:100px;">Start</button></td>
</tr>
<?php $i++; } $_SESSION['exam']=$i; ?>
</table>
</form>
Name your submit button, (also make it a submit type) and assign the paper code to its value attribute.
<button type="submit" style="width:100px;" name="clicked" value="<?=$row['paper_code']?>">
Start
</button>
Now, in exam_page.php you can get the value of the clicked button from $_POST['clicked']. (Or whatever you decide to name it.)
To get the values from the other inputs associated with the button you clicked, you can add the paper code to their names instead of using $i.
<input type="text" value="<?=$row['time']?>" name="time[<?=$row['paper_code']?>]">
and in exam_page.php you can get the value from $_POST['time'][$_POST['clicked']], etc.
If they aren't intended to be editable in your form, though, I would recommend using something else to display them and just loading them from the database in exam_page.php instead. Otherwise, your users will be able to override the readonly attribute and submit different values.
Try using javascript onclick functions:
<script>
function getPaperCode(paperCode)
{
alert(paperCode);
}
</script>
Then edit your input add onclick event:
<input type="text" value="<?php echo $row['paper_code']; ?>" onclick="getPaperCode('<?php echo $row["paper_code"]; ?>');" name="paper_code<?php echo $i; ?>" readonly><?=$row['paper_name']?>
Once you click. it will alert the value of the button
passed your unique id value or examcode via hidden field like this
<input type="hidden" name="id" value="<?=$row['eid']?>">
and on button click perform query like
$id=$_POST['id'];
select * from table_name where id='$id';
i want to get the text box value which i selected from array of text boxes..
My code is like this:
Here am displaying the data on the browser from DB..
<?php
foreach($dataDecoded['categories'] as $key => $value)
{
?>
<tr>
<td><?php echo $value['CategoryName'];?></td>
<td>
<input type="text" name="categoryId[]" id="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName[]" id="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" id="create" value="create">
</td>
</tr>
<?php
}
?>
The data will be displayed like:
categoryId1 categoryName1 <Create Button>
categoryId2 categoryName2 <Create Button>
categoryId3 categoryName3 <Create Button>
and so on like this..
Now, suppose When i click on Create Button of CategoryName2, i want to get the categoryId and categoryName of only CategoryName2..
Am using this code:
if(isset($_POST['create']) && $_POST['create']=="create")
{
$cat_id[]=$_POST['categoryId'];
$cat_name[]=$_POST['categoryName'];
}
But with this, am getting all the categoryId and categoryName into the array.. how to get only the selected textbox value..
I want to do it using only PHP and not with JavaScript / JQuery... Am a bit new to PHP... Can someone help me / give me a suggestion about how to do it...
Thanks in advance.
Wrap each one in its own form:
<td>
<form method="POST" action="somePage.php">
<input type="text" name="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" value="create">
</form>
</td>
Since the content being posted in this case is just these two values, then that's the entire scope of a single form post. So each one is logically/semantically its own form. (There's no rule that says a page can have only one form. Unless you're ever using ASP.NET WebForms, which is a lie.)
Note also that I removed your id attributes. Multiple elements can't have the same id, that's invalid HTML and any behavior as a result becomes undefined.
I have a table with radio buttons to get the row values and 2 buttons
1 button.)For printing data , which moves to "notice.php"
2 button.)For row details,which stays on the same page.
<form action="" method="POST">
<table border="1" >
<tr>
<th>sourceID</th>
....
<th>Status</th>
</tr>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
<td>1</td>
....
<td>open</td>
</tr>
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
<input type="submit" name="details" value="details" />
<?php
if(isset($_POST['details']))
{
$n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
}
Notice.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];
}?>
The problem here is: My code is working perfectly fine with the button details.
But it doesnt work with issue, i.e after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\XAMPP\notice.php.
kindly help
Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.
You could include the data on the query string and send it that way, e.g.:
location.href="notice.php?id=<?=$tot?>"
Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.
Using the following code is the exact same as having a link:
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
As in, this will not change the form action and submit the POST data to your new page.
You would need something like:
<form method="post" action="" name="unique-form-name">
<input type="radio" name="ID[]" value="<?php echo $tot; ?>">
<input type="button" id="unique-btn-name" value="Issue Notice">
</form>
<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
document['unique-form-name'].action='notice.php';
document['unique-form-name'].submit();
}
</script>
Then, once you get the data to notice.php, you'll have to use the data as an array (you won't be able to echo the data):
$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />
Your error is the name attribute.
Also the other button is not related to the form at all. You may want to use ajax here.
I have a table, that generates rows dynamically. I have used radio buttons for each row. I want the radio button to be checked even after I click my submit button.
This is body of my table:
<form action="" method="POST">
<table>
<thead>
<tr>
<td></td>
<td><h3>Date<h3></td>
<td><h3>Status<h3></td>
</tr>
</thead>
<tbody>
<?php for($i=$start;$i<$end;$i++)
{
$add=$ARRAY[$i]['source_address'];
$Status=$ARRAY[$i]['Status'];
$total=$add.$Status;
?>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $total; ?>"/></td>
<?php
echo'<td>'.$ARRAY[$i]['escl_date'].'</td>';
echo'<td>'.$ARRAY[$i]['escl_status'].'</td>';
?>
</tr>
<?php }?>
</tbody>
</table>
<input type="submit" name="details" value="details" />
How Can I make the radio button selected even after clicking on submit button? Please help me.
You can try this, You need a condition to check with reference value and add checked attribute.
Here your reference value is your POST of ID value. Add this into your radio tag <?php echo $_POST['ID'][0]==$total ? 'checked':'';?>
<input type="radio" name="ID[]" value="<?php echo $total; ?>" <?php echo $_POST['ID'][0]==$total ? 'checked':'';?> />
The correct HTML attribute you are looking for is checked.
Note to other answer: readonly is not necessary, nor is it what OP is asking for.
Since you're doing it in PHP, let's say you're passing something with an HTML markup name of foobar, as follows:
<form><input type="radio" name="foobar"></form>
To make sure that the value is retained even after it is submitted, you could add a code like this (get or post depending on your purpose):
<form><input type="radio" name="foobar" <?php if(isset($_GET[foobar])){ echo "checked"; ?>></form>
This is to make sure that it would be checked if the name was passed in the form.
Note that this is just an example.
Try this, it works:
<input type="radio" checked>
We've inherited a php/CodeIgniter app. Without going into all the reasons why, I need to feed values into a textarea field so I can group a bunch of data together and send it to a field in another app/outside vendor. This is the first time we have encountered this issue, but I don't think it is the last, so I want to prepare for it.
The specifics:
Web form with a bunch of fields on it. It's a self generating php/CodeIgniter app that the client controls, so fields are different from client to client.
Certain clients may need to send data from 3, 5, 7, etc., of the field within their form to an external vendor who receives all the data in one field on their end. So in short, using jQuery, I want to send data from certain fields to a textarea field.
For example, I want to send Center Title, Full Name, and Fruits to the textarea field with a line break at the end of each. Of course, if the user empties a field, that line item would be removed from the textarea field.
Click here to view my jsFiddle demo.
HTML Example:
<form method="post">
<br />
<br />
<fieldset name="Group1" style="border: thin; border-color: green">
<legend>General Information</legend>
<table style="width: 100%">
<tr>
<td style="width: 249px">Center Title:</td>
<td>
<select name="centers" id="centers">
<option value="Corp 1">Corp 1</option>
<option value="Shamrock Gold">Shamrock Gold</option>
<option value="Hensin Way">Hensin Way</option>
</select>
</td>
</tr>
<tr>
<td style="width: 249px">Full Name:</td>
<td>
<input name="fullname" id="fullname" type="text" size="20" />
</td>
</tr>
<tr>
<td style="width: 249px">Job Title:</td>
<td>
<input name="jobtitle" id="jobtitle" type="text" />
</td>
</tr>
<tr>
<td style="width: 249px">Known Alergies:</td>
<td>
<input name="knownAllergies" id="knownAllergies" type="checkbox" value="Yes" />Yes
<input name="knownAllergies" id="knownAllergies" type="checkbox" value="No" />No
</td>
</tr>
<tr>
<td style="width: 249px; height: 102px;">How Many?:</td>
<td style="height: 102px">
<select multiple="multiple" name="Select2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
</td>
</tr>
<tr>
<td style="width: 249px">Fruits:</td>
<td>
<input name="Fruit[]" id="Fruit[]" type="radio" checked="checked" value="Apple" />Apple<br />
<input name="Fruit[]" id="Fruit[]" type="radio" value="Orange" />Orange<br />
<input name="Fruit[]" id="Fruit[]" type="radio" value="Fruit" />Fruit
</td>
</tr>
</table>
<label>Complete Info:</label>
<textarea name="allVendorInfo" id="allVendorInfo" cols="50" rows="7"></textarea><br />
<br />
</fieldset>
</form>
You can access selections/user-input using following code
$(document).ready(function(){
$("input").change(function(e){
if($(this).is(':checked')){
}else{
};
var old = $('#allVendorInfo').val();
$('#allVendorInfo').val(old+ "%" + $(this).attr('name') + '|'+$(this).val() + "%");
});
$("select").change(function(e){
var old = $('#allVendorInfo').val();
$('#allVendorInfo').val(old+ "%" + $(this).attr('name') + '|'+$(this).val() + "%");
});
});
EDIT: Original fiddle was broken for some reason, have updated the link
I was thinking something along the lines of this: http://jsfiddle.net/JRwzz/3/
The 'trigger' for this could be something other than the Run JS button of course, that's just there for example, i'd imagine it'd be on submit or other user action.
It clears the textarea first, then loops all of the input's and selects, depending on the type of element - checkbox, radio, select etc.. it uses slightly different methods to get the values (e.g. if it's a checkbox it only wants to get the value from a checked one)
It'll need a bit of polish in order to have a checkbox group's values all on one line and things like that, but hopefully this is enough for you to get the idea.
Then on each thing it finds it appends it to the textarea and puts a linebreak on the end.
It wouldn't be too hard to add a condition to check for another atribute (e.g. data-export="yes") to check for before including it in the textarea.
Just to note, I thought of it this way because you said all of the forms are dynamic, so I tried not to need to rely on ID's or names for things, it'll just apply to any form. If you can get the code that generates your forms to output an attribute in the html on the ones you want included in your textarea (and perhaps some method of your client selecting which ones that'll apply to in their administration area) then that'd be spot on, would save having to fiddle JS for every client.
Try something like this:
$("select, input").bind("change keyup", function() {
var form = $("<form>");
var data = new Object();
form.append($(":checked").clone());
$.each($("select"), function(i, item) {
if ($(item).val() != null) {
form.append(($(item).clone()));
}
});
$.each($("input[type='text']"), function(i, item) {
if ($(item).val().length > 0) form.append(($(item).clone()));
});
$("#allVendorInfo").val(form.serialize());
}).first().trigger("change");