set $_POST value when radio button is selected - php

I have a page with some radio buttons and form. When I select one radio button I need its id to be set to $_POST['id'] varible so when I submit the form I can check which radio button was selected and act accordingly. I know I can't do it directly so tried using
onclick="document.getElementById("id").value="'.$row['id'].'"
on radio button but it didn't work.
$d="select * from {$_c['dbprefix']}card where active='1' order by s_sent desc, s_order asc limit 0,{$_c['filtr']['step']}";
$r=mysql_query($d);
if (mysql_num_rows($r)>0) {
echo '<div class="cards">';
echo '<form method="POST">';
$i=0;
while ($row=mysql_fetch_array($r)):
echo '<div class="item">';
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
echo '<label for="'.$row['id'].'" class="itemin" style="height:'.$_c['card']['sy'].'px"><img src="pub/cards/'.$row['id'].'s.jpg" /></lable>';
echo '</div>';
$i++;
if ($i%3==0) echo '<div class="cl-left"></div>';
endwhile;
echo '<div class="cl-left"></div>';
echo '</div>';
}
?>
<div class="dvasl">
<div class="sl1">
<fieldset>
<legend>Saatja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[from_name]" value="<?php echo $ap['set']['from_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[from_email]" value="<?php echo $ap['set']['from_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Saaja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[to_name]" value="<?php echo $ap['set']['to_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[to_email]" value="<?php echo $ap['set']['to_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
</div>
<div class="sl2">
<fieldset>
<legend>E-kaardi tekst</legend>
<table class="info">
<tr>
<td><textarea name="item[text]" cols="20" rows="8" class="inpedit2"></textarea></td>
</tr>
</table>
</fieldset>
</div>
<div class="cl-left"></div>
</div>
<div class="buttons">
<div class="t2"><input type="submit" value="Vaata kaardi eelvaadet" name="button[nahled]" class="unbutt1"></div>
</div>
<input type="hidden" name="id" value="<?php echo $ap['card']['id']; ?>">
<input type="hidden" name="action" value="itemadd">
</form>
<?php
$k=floor(($i-1)/3);
if ($k<2) echo '<div class="spacer-'.(2-$k).'"></div>';
?>

Its better if you can use a hidden field in the form and update its value with the id of the selected radio button and when the form will be posted you can get its value and more over this after once you changed the value of the hidden field you can access it with javascript also with id attibute and can get it in the post by name attribute
add a hidden field named
<input type="hidden" id="checked_radio" name="checked_radio" value="" />
and then change the value with javascript on click of button
<input type="radio" id="foo" name="foo" onclick="document.getElementById('checked_radio').value=this.value;" />

The value of the selected radio button gets posted, and the name is used to group radio buttons together. (Only one radio button with the specified name can be selected.) Instead of trying to post the value of the id attribute you should add a value attribute and set that. You could set it to the same value as the id attribute, or remove the id attribute completely.

When I select one radio button I need its id to be set to $_POST['id'] variable
You are changing the value, not the id, to change the id use:
onclick="document.getElementById("id").id="<?php echo $row['id'] ?>";
Anyway, I think you should use the name attribute for this.

change this line
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
to
echo '<input id="'.$row['id'].'" type="radio" value="'.$row['id'].'" name="id" class="detail">'.$_l['detailtitle'].'</input>';
when your form submit,you will get the selected id as $_POST['id']

Related

Posts are not saved through the checkbox

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.

How to Keep the data that user entered in textbox even after submit button is clicked and even if page is reloaded

I want to keep the data in a text box after submit and inserted into db.
even if i refresh the page data should not change or empty
here is my code
<tr class="form-field1">
<th valign="top" scope="row">
<label for="approved_admin"><?php _e('Collaboration Admin', 'custom_table_example')?></label>
</th>
<td>
<input id="approved_admin" name="approved_admin" type="email" value="<?php echo esc_attr($item['approved_admin'])?>"size="50" class="code" placeholder="<?php _e('Admin Email', 'custom_table_example')?>" required>
</td>
</tr>
if i do this data will be there if i reload empty text box will be shown.I need to make this readonly and onload same text should be there.
Form
<form id="form" method="POST">
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
<?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
<input type="hidden" name="sid" value="<?php echo $item['sid'] ?>"/>
<div class="metabox-holder" id="poststuff">
<div id="post-body">
<div id="post-body-content">
<?php /* And here we call our custom meta box */ ?>
<?php do_meta_boxes('Settings', 'normal', $item); ?>
<input type="submit" value="<?php _e('Save', 'custom_table_example')?>" id="submit" class="button-primary" name="submit" / >
<input type="submit" value="<?php _e('Edit', '')?>" id="edit" class="button-primary" name="edit" / >
</div>
</div>
</div>
</form>
I tried doing
<input type="email" name="approved_admin" value="<?php if (isset($_POST['approved_admin'])) { echo $_POST['approved_admin']; } ?>">
and also
<input type="text" name="country" value="Norway" <?php isset($approved_admin) ? echo 'readonly' : ''; ?> >
You can save data after page reload a lot of ways like... Session , cookie, get , post. After this value get by isset and echo .
In your case Post method used..
Try this working fine all thing
<input type="text" name="country" value=" <?php echo isset($_POST['country']) ? $_POST['country'] : ''; ?>" <?php echo isset($_POST['country']) ? 'readonly' : ''; ?> >
You just put field name in isset();
You can use this syntax. Have a try for this.
<input type="text" id="to_date" name="to_date" class="form-control" placeholder="Enter End Date" value="<?php echo isset($_POST['to_date']) ? $_POST['to_date'] : '' ?>" />
The name of the input type has to be mentioned in the isset() condition.

Getting post data from selected rows in a table

I've been trying to think this through, but can't seem to figure out how to get data from only one selected row in a table. I have a table that I am populating with php, lets say it has a name, tn, address in one column. I was thinking of putting a checkbox at the end of the column and having the user check it if the data can be sent to the server. Now, if I do that how do I get the data from that column? I could use javascript to populate the table also, but I'm still stuck with the same problem of how to get the data from the column to update my tables.
<?php
foreach (defaultPay() as $key => $values):
foreach ($values as $value):?>
<tr>
<td>
<label for="number"></label><input type="text" id="number" name="empNum" value="<?= $value['empNum']; ?>">
</td>
<td>
<label for="name"></label><input type="text" id="name" name="empName" value="<?= $value['empName']; ?>">
</td>
<td>
<label for="stdHrs"></label><input type="text" step="any" id="stdHrs" name="stdHrs" value="<?= $value['unitRate']; ?>">
</td>
<td>
<label for="uniRate"></label><input type="text" step="any" id="uniRate" name="uniRate" value="<?= $value['rate']; ?>">
</td>
<td>
<label for="salRate"></label><input type="text" step="any" id="salRate" name="salRate" value="<?= $value['salary']; ?>">
</td>
<td>
<label for="gross"></label><input type="text" step="any" id="gross" name="gross" value="<?= $value['gross']; ?>">
</td>
</tr>
<?php }
} ?>
I suppose you are populating the table from some kind of php array. If you don't have unique identifier, each element have index in array. You could use that index and set it as checkbox value.
For example:
<input type="checkbox" value="<?php echo $index; ?>" />
where $index is unique table index. After form submition you will have selected indexes of the table. Remember to keep te table order the same.
If only one row will be selected by the user a button within a form will do. Forms and hidden input does not affect page rendering. Just make the form CSS display:inline; The whole form can be put in one small <td> table element regardless of how much data you want to store in multiple <input type="hidden"/> tags.
'<tr><td><form action="save.php" method="post" ><input type="hidden" name="id" value="1234" /><input type="hidden" name="col1" value="' . $row[1] . '" /><input type="hidden" name="col2" value="' . $row[2] . '" /> <button type="submit">Save</button></form></td><td>Text describing the row</td></tr>';
The first column in the table has a "Save" button which submits the data.

Checking the correct answer from the database by a checked radio button in PHP

I am creating an Online Assessment. I display all the questions randomly in a one page only. I have difficulty on how to check the correct answer in the database for checked radio button. I don't know what to do and the logic on how to do it.
This is my php codes for displaying the questions randomly,
$view_questions=mysql_query("SELECT * FROM questions ORDER BY RAND()");
This is my html codes with php codes,
<form name="" method="POST">
</br><h4># of Questions</h4>
<?php
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="view_question fsize">
<p align="justify"><?php echo $i;?>) <?php echo $row['QUESTION'];?></p>
<div class="indent-question">
<input type="radio" value="1" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_1'];?>
</br>
<input type="radio" value="2" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_2'];?>
</br>
<input type="radio" value="3" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_3'];?>
</br>
<input type="radio" value="4" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_4'];?>
</div>
</div>
<?php
$i++;
}
?>
<center><button id='next<?php echo $i;?>' class='next btn btn-success' name="finish" type='submit'>Finish</button></center>
</form>
Table name: questions
Table fields: QUESTION_NO, QUESTION, ANSWER_1, ANSWER_2, ANSWER_3, ANSWER_4, ANSWER
It is very easy
store Id of you questions in the value in the radio button and checkbox only those value are submitted which are checked , compare your code after submitting the form in next page , I hope you will understand, What i am trying to say
You should have database structure like this..
Table 1: Questions
Fields: que_id, question
Table 2: Answers
Fields: ans_id, que_id, answer, correct_ans, points
Table 3: Results
Fields: que_id, ans_id
When you add question, question and que_id will be stored n database.. Then you'll add multiple possible answers which are stored in Answers table with reference to que_id..
You need to change query for GUI to fetch question and answer from different table using join.
So GUI would be like this..
<form name="" method="POST">
</br><h4># of Questions</h4>
<?php
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="view_question fsize">
<p align="justify"><?php echo $i;?>) <?php echo $row['QUESTION'];?></p>
<div class="indent-question">
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_1'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_2'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_3'];?>
</br>
<input type="radio" value="<?php echo $row['ans_id']; ?>" id="" name="radio[<?php echo $row['QUESTION_NO'];?>]"> <?php echo $row['ANSWER_4'];?>
</div>
</div>
<?php
$i++;
}
?>
<center><button id='next<?php echo $i;?>' class='next btn btn-success' name="finish" type='submit'>Finish</button></center>
</form>
After that when user select an answer, ans_id will be saved along with the que_id in results table, from there you can manage all the information and comparison..
set Answerquestions to value for radio button
<input type="radio" value="ANSWER1" name="name">
<input type="radio" value="ANSWER2" name="name">
in php code
check the value submited with the correct Answer
get the correct Answer from db and save it in var
$query "SELECT correct_answer FROM TABEL_NAME";
and fetch query in var correct_a for example
$user_answer = $_POST['name'];
if($user_answer == $correct_a)
return true
else
return false

checkbox says only "on" all the time

this is how I have 2 checkbox and I do not want that you should only click on them simultaneously. it must only be possible to click on one at a time!. in the present to when I clicked the news, so it must enter a number in there to write "on". I would like to later be sure it is entered into the database.
the problem is that it appears to say "on" no matter what!?
<form action="#" method="post">
<table>
<tr>
<td>Emne</td>
<td>Vigtigt: <input type="checkbox" name="vigtigt" class="new"> Nyhede: <input type="checkbox" name="nyhede" class="new"></td>
</tr>
<tr>
<td>Title</td>
<td><input type="text" name="title" maxlength="50" class="new"></td>
</tr>
<tr>
<td>Tekst</td>
<td><textarea name="tekst" cols="20" rows="15" class="new"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="opret" value="Opret Blog" class="new"></td>
<td></td>
</tr>
</table>
<?php
if(isset($_POST["opret"]))
{
if($_POST["vigtigt"] != "")
{
echo $_POST["vigtigt"];
echo "<br />";
echo $_POST["nyhede"];
}
elseif ($_POST["nyhede"] != "")
{
echo $_POST["vigtigt"];
echo "<br />";
echo $_POST["nyhede"];
}
else
{
echo "Fejl!";
}
}
?>
</form>
You have to define a value attribute for it to:
<input type="checkbox" name="vigtigt" class="new" value="MyValueHere">
This way you will receive its value when its checked.
Edit:
To have only one selected at a time you will have to use type="radio" and give them the same name and different values.
<input type="radio" name="vigtigt" class="new" value="MyValueHere">
<input type="radio" name="vigtigt" class="new" value="MyOtherValueHere">
The purpose of checkboxes if to provide a group of options from which any number can be selected.
If you want a single option to be selected, use radio buttons (or a select element).
The value attribute (of the checked radio / selected option) determines the value that will be submitted. on is the default value for checkboxes which is used if you forget to specify a specific value.
<label><input type="checkbox" name="foo" value="vigtigt" class="new"> Vigtigt</label>
<label><input type="checkbox" name="foo" value="nyhede" class="new"> Nyhede</label>

Categories