Validation HTML Radio Button - php

I am facing a problem with simple validating of radio button though database.
What I want to do is to just simply check whether radio button for each question is selected or not. I know the simple checking. But as I am using php variable name for every radio button name, it's very hard to convert to javascript variable and check. And I am stuck at this point.
<form id="formID" method="post" action="user_anger_quiz.php?rating_finished=true">
<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,1"/>
<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,2" />
<p id="anger_rating_submit">
<input type="submit" value="Rate my anger level"/></p>
</form>
Any suggestions would be appreciated in advanced.

regarding to your comment:
I guess that you will use php to check it due to the fact that you just add php as a tag.
In php you can use $_POST or $_GET on action page of your form to fetch the radio buttons.
Otherwise you can use javascript / javascript library

I think you're asking to make sure there is a radio button checked before submitting? Try the below untested function using jquery to see if there is a radio button selected. Run that before the form is submitted.
function somethingChecked()
{
if (!$("input[name='<?php echo $question_form_name; ?>']:checked").val()) {
alert('You need to check something');
return false;
} else {
return true;
}
}

Related

how to uncheck checkboxes when another is checked?

im kinda trying to get into programming in general and was wondering how to uncheck / check with updating the array-
like as soon as someone checks a 2nd checkbox it should uncheck the first option and update the search (w the new data)- im a mere beginner and kinda lost rn so would appreciate any form of help
<form action="index.php" id="form1" name="form1" method="get">
<?php $i = 0; foreach ($row_page_nav_kategorie as $row_page_nav_kategorie) { ?>
<label class="checkbox-container">
<input <?php if (strpos($url,$row_page_nav_kategorie['typ']) == true) {echo 'checked="checked"';}?>
type="checkbox"
class="checkmark"
name="hotelKategorie[]"
id="ckb5"
value="<?php echo $row_page_nav_kategorie['typ']; ?>"
onclick="kategorie(<?php echo $i ?>);"
onchange="submit()"/>
<?php echo $row_page_nav_kategorie['typ']; $i++;?>
</label>
<?php } ?>
</form>
You should use radio buttons, but if you want to overwrite checkbox functionality below code will take care of it, I have added a common class on your inputs:
function checkboxClick(obj) {
var cbs = document.getElementsByClassName("checkkbox-option");
for (var i = 0; i < cbs.length; i++) {
cbs[i].checked = false;
}
obj.checked = true;
}
Demo
First of all welcome to the community!
As for your question, there is multiple ways to handle this, one of wich is as followed:
In HTML there's an attribute called radio wich you can add to your input by using type='radio'. In a set of radio buttons, only one can be checked at any time. If you then want to immedietely submit your form, you can use something like onChange='this.form.submit()'. This will submit your form when the value is changed, such as pressing on a different radio button.
Something to keep note of is that the attribute onChange is case sensitive as far as i'm aware. You were heading in the right direction with onchange="submit(), but your code doesn't know what to submit. this.form.submit() will submit the form that the element is in.
Use Radio Buttons or use JavaScript on your page to dynamically uncheck other checkboxes when you click on one.
If you want to dynamically update the page content with the new search results you should also look into AJAX which basically means you will call PHP functions from JavaScript code and those will return JSON arrays that you can exploit to modify your page's DOM.
Try THIS
HTML:
<label><input type="checkbox" name="check1" class="checkbox" /> CheckBox1</label>
<label><input type="checkbox" name="check2" class="checkbox" /> CheckBox2</label>
<label><input type="checkbox" name="check3" class="checkbox" /> CheckBox3</label>
<label><input type="checkbox" name="check4" class="checkbox" /> CheckBox4</label>
jQuery:
$(".checkbox").change(function() {
$(".checkbox").prop('checked', false);
$(this).prop('checked', true);
});
if you want to uncheck the selected checkbox
$(".checkbox").change(function() {
$(".checkbox").not(this).prop('checked', false);
});
hope this helps, thanks !!!

How can I check if a checkbox is NOT checked

I need to check whether checkbox is checked or not. Normally I would do it like this:
<?php
$checked = isset($_POST['checkbox']);
?>
But I don't know what is the name. More at screenshot (I'm using Laravel 4).
Screenshot
You simply can't. The data for unchecked checkboxes are not send to the server.
You could do a workaround with javascript where the JS appends some hidden fields before submit with the nonchecked boxes
Supposedly you should know what the list of checkboxes is/was that you asked the user to check. Checked checkboxes are submitted to the server, unchecked ones aren't. You can calculate the difference between these two lists.
if you are using jquery, and you know the id of the checkbox,
then, you can detect with following code:
var isChecked = $("#cbId").is(":checked");
<?php
if ( ! isset($_POST['checkbox_name']))
{
"Not checked";
}
?>
If checkbox didn't check - you will not have this variable in $_REQUEST.
<form action="">
<input type="checkbox" name="ch1"/>
<input type="checkbox" name="ch2"/>
<input type="checkbox" checked="checked" name="ch3"/>
<input type="submit" name="Post" value="Post">
</form>
When you click on "Post". In backend you'll see:
<?php
if(isset($_REQUEST['ch1']))
echo 'ch1 is checked!';
if(isset($_REQUEST['ch2']))
echo 'ch2 is checked!';
if(isset($_REQUEST['ch3']))
echo 'ch3 is checked!';
?>
In my case you'll see: "ch3 is checked!".

How to POST radio button value in php

I am doing project using Zend framework. here within the form I add field using radio buttons. after the form post. it doesn't send that radio button value(but other fields (eg -text field can post)). this is my code in the view.
<form class="custom" method="post">
<?php
foreach ($answers as $answer) {
echo '<input name="q_answer" value="'.$answer.'" type="radio" >'.$answer;
}
?>
<input class="small secondary button" type="submit" value=" Ok ">
</form>
this is my code within controller
if($request->isPost()){
$ans = $_POST['q_answer'];
}
so when I post the form. it gives Undefined index: q_answer error. what is the wrong. please help me.( within the controller I print posted values using var_dump but 'q_answer' value not available)
If no option is selected this field is not appear in $_POST. So you should first check with isset() if it is present and the try to process. And while you are using ZF, you should use getPost()instead of digging directly in $_POST:
$ans = getPost( 'q_answer', 'default-value-if-no-element-is-found' );

$_POST, image forms and mysql.How to get them working together?

I'm trying to get a website working. What I have are basically two images displayed (random, taken out of a mySQL database). What I need to do is (when the user clicks one of the images) the following:
Update the page, passing the info about the selected image (submit form);
Add one piece of data to the database (upvote the image)
I need to use $_POST to pass an array of values to the next page. So I thought:
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image.png"
value ="dat1[\"data1\",\"data2\",\"data3\"]">
<!-- If value must be a single string, I'll use hidden inputs-->
</form>
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image2.png"
value ="dat2[\"data1\",\"data2\",\"data3\"]">
</form>
Then I can upvote the selected image on the mySQL database with a little php upvote() function that updates the record. The upvoting process is done when the new page is loaded. From this, I have a couple questions:
I'm guessing the images will act as buttons, right? (They are supposed to submit the form, hence refreshing the page). If not, how can I achieve this? I'm unable to do it with a link (since I can't add the values to it). Maybe a javascript function? But I don't know how to submit the form that way either...
Once the page is reloaded, does it mean that only the data from one form has been submited, so I can retrieve the data by simply calling the PHP variable $_POST['img'] and get an array back?
EDIT: I now managed to get everything working, slightly similar to what I proposed initially. Thanks for the AJAX suggestion though, since it was what helped me solve it (looked up AJAX tutorials, found solution).
Here's my solution:
<?php
echo "<form name=\"input\" action=\"F2F.php\" method=\"POST\">";
echo "<input type=\"hidden\" name =\"table\" value=\"".$table1."\">";
echo "<input type=\"image\" name=\"nom\" src=\"".$IMG_Route1."\" value =\"".$Nom_base1."\" border=\"0\">";
echo "</form>";
?>
(where the image goes)
and then, on the header:
<?php
if ($_POST['nom']||$_POST['nom_x']){
if (!$_POST['nom']){
echo 'Could not retrieve name. $_POST[\'nom_x\'] = '.$_POST['nom_x']. mysql_error();
exit;
}
if (!$_POST['table']){
echo 'Could not retrieve table. $_POST[\'table\'] = '.$_POST['table']. mysql_error();
exit;
}
upvote($_POST['table'],$_POST['nom']);
}
?>
You can use one form and a set of radio buttons to simplify things a bit. Clicking on the label will toggle the radio button. You can use commas to separate multiple values for each checkbox, which you can then abstract later on (see below)
<form name="input" action="the_page.php" method="POST">
<ul>
<li>
<label>
<img src="whatever.jpg" />
<input type="radio" name="selectedImage" id="img1" value="12,16,19" />
</label>
</li>
<li>
<label>
<img src="whatever2.jpg" />
<input type="radio" name="selectedImage" id="img2" value="12,16,19" />
</label>
</li>
</ul>
</form>
You can detect when the radio button is selected by adding a listener for the change event, then submit the form.
$('input[name="selectedImage"]').change(function() {
$('form[name="input"]').submit();
});
To abstract the multiple values, you can then explode the form result with PHP, which will return an array of the values.
$selectedImageValues = array();
$selectedImageValues = explode(",", $_POST['selectedImage']);
From there you can pull the different values out and save the data to the database.

radio button value in php

I'm trying to make a simple survey in php
I have a set of radio buttons on a page called sja.php that sends its to sjamail.php page
the problem is that when I go to get
$answer = $_POST['ans'];
I can't seen to do anything like
echo "$answer";
but if I were to throw some logic at it
like
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
It will display correct or incorrect (edit: The if/else works correctly and will display the correct answer )
so why is it I can't access the value of the radio button "ans" as a string?
http://www.markonsolutions.com/sja.php
print_r($_POST); will return Array ( [ans] => )
Perhaps the value is something other than text.
Try
var_dump($answer);
or
print_r($answer, TRUE);
Your page works correctly if you select any of the first 4 radio buttons (ans1/2/3/4). But the rest of the radio buttons next to all those images have blank values, which would explain why your posted value is empty if you selected any of those to test with.
You need to make sure that the field in HTML has...
<input type="radio" name="ans" value="ans1" />
<input type="radio" name="ans" value="ans2" />
Also make sure your form method is POST
I had a similar problem with the following:
<input name="03 - Gender" type="radio" value="Masculino"/>Male<br/>
<input name="03 - Gender" type="radio" value="Femenino" required="required"/>Female <br/>
<input type="hidden" name="03 - Gender" value=""/>
but when I removed the third input line (the hidden one) the problem desapeared.
Try this:
$answer = (string)$_POST["ans"];
echo $answer;
You must convert $_POST["ans"] to string.

Categories