Submit radio form without javascript - php

I have the following code :
<div id="choices">
<div>
<input type="radio" name="question-answers" id="question-answers-A" value="A" onclick="this.form.submit();" />
<label for="question-answers-A">$c[0] </label>
</div>
<div>
<input type="radio" name="question-answers" id="question-answers-B" value="B" onclick="this.form.submit();"/>
<label for="question-answers-B">$c[1]</label>
</div>
</div>
The form is submitted when a radio is selected.
Is there a way to do it without javascript? (without onClick)
Thanks.

Why not the traditional submit input ?
<form>
<input type="radio" name="r1">
<input type="radio" name="r2">
<input type="submit" value="send" />
</form>
The form is submitted when a radio is selected.
Javascript is the way to go or plain old html but it will not be submitted on change.

There is no way to detect when a radio button is clicked without using Javascript.

Here's an example of getting it to work without Javascript http://jsfiddle.net/DCHaT/8/
You rely on CSS and labels.
CSS:
button.transparent{
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
visiblity:hidden;
border:none;
z-index:2;
background-color:transparent;
}
div.radio{
position:relative;
}
div.radio > label > input[type="radio"]{
position:relative;
z-index:1;
}
Code:
<form action="http://www.yahoo.com" method="GET">
<div class="radio">
<label for="radio_1">
<button type="submit" class="transparent" name="radio" value="1"></button>
<input type="radio" value="1" name="radio" id="radio_1"/>
Radio 1</label>
</div>
<div class="radio">
<label for="radio_2">
<button type="submit" class="transparent" name="radio" value="2"></button>
<input type="radio" value="2" name="radio" id="radio_2"/>
Radio 2</label>
</div>
</form>

You could use php
<div class="field form-inline radio">
<label class="radio" for="txtContact">Preferred Method of Contact</label>
<input class="radio" type="radio" name="contact" value="email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="phone" /> <span>Phone</span>
</div>
$contact = $_POST['contact']
//Will return either "email" or "phone".

Use two submit buttons, and optionally style them to look like radio buttons. http://jsfiddle.net/trevordixon/FNzhb/3/
<form method="POST">
<button type="submit" name="question-answers" value="A">A</button>
<button type="submit" name="question-answers" value="B">B</button>
</form>
<style>
button {
border: none;
background: none;
padding-left: 10px;
cursor: pointer;
}
button:before {
content: '';
display: block;
width: 12px;
height: 12px;
border: 1px solid #999;
border-radius: 50%;
}
button:active:before { content: '•'; }
</style>

Related

Require Radio Button answer before form submission

I have an assessment form that has true and false radio button options. I have set the required field, however it is still submitting and proceeding to the results page even if no answer is chosen. What am I missing? Here is the code for the form:
<form action="../results" method="post" id="assessment">
<!-- Question 1 BLUE -->
<div class="evaluation_question">Sometimes I feel and experience moments of anger "out of the blue".</div>
<div class="evaluation_answers">
<span class="answer_choice">
<input type="radio" class="radio_button" name="question1" id="question1_1" value="True" required/>
<label class="radio_label" for="question_1">True</label>
</span>
<span class="answer_choice">
<input type="radio" class="radio_button" name="question1" id="question1_2" value="False" />
<label class="radio_label" for="question_2">False</label>
</span>
<input type="radio" style="display: none;" name="question1" id="question1_none" checked="checked" value="" />
</div>
<input type="submit" value="Submit" class="submitbtn" />
</form>
<form action="../results" method="post" id="assessment">
<!-- Question 1 BLUE -->
<div class="evaluation_question">Sometimes I feel and experience moments of anger "out of the blue".</div>
<div class="evaluation_answers">
<span class="answer_choice">
<input type="radio" class="radio_button" name="question1" id="question1_1" value="True" required/>
<label class="radio_label" for="question_1">True</label>
</span>
<span class="answer_choice">
<input type="radio" class="radio_button" name="question1" id="question1_2" value="False" />
<label class="radio_label" for="question_2">False</label>
</span>
</div>
<input type="submit" value="Submit" class="submitbtn" />
</form>
Remove the <input> the checked = "checked". You have made a invisible radio button that has already been selected, so the form is not "empty". None of the others will be selected if you do that.
I think you have unnecessarily complicated this
const myForm = document.querySelector('#assessment')
myForm.onsubmit = e =>
{
e.preventDefault()
console.clear()
console.log( Object.fromEntries( new FormData(myForm).entries() ) )
}
myForm.onreset = () =>
{
console.clear()
}
form > div {
margin : 1em
}
.evaluation_answers span {
border : 1px solid grey;
padding : .2em .4em;
}
.evaluation_answers label input {
position : fixed;
opacity : 0;
pointer-events : none;
}
.evaluation_answers label:nth-child(1) > input:checked + span {
background-color : lightgreen;
}
.evaluation_answers label:nth-child(2) > input:checked + span {
background-color : lightcoral;
}
<form action="../results" method="post" id="assessment">
<!-- Question 1 BLUE -->
<div class="evaluation_question">
Sometimes I feel and experience moments of anger "out of the blue".
</div>
<div class="evaluation_answers">
<label>
<input type="radio" name="question1" value="True" required>
<span>True</span>
</label>
<label>
<input type="radio" name="question1" value="False">
<span>False</span>
</label>
</div>
<button>submit</button>
<button type="reset">reset</button>
</form>

Value of Radio Button not print in php

I created a simple quiz program using only php and there are 4 different question and radio buttons and when i click any of option then print the value of radio buttons like A or B or C or D I created 4 php variable, assigned the values by $_POST method and i want to print these values by echo. But Something is not being printed Please Help..
1. <style type="text/css"> body { background-color: grey; }
#computer , #politics { cursor: pointer; background:
linear-gradient(to right,blue,#bfbfe0); color: #fff; padding:
0px 0px 0px 5px; border-radius: 5px; width: 100%; }
.computer_qsnans , .politics_qsnans { display: ; }
</style> </head> <body>
<div class="main"> <form method="GET" action="index.php"> <div
id="computer"> <p>
<h1>
Technology
</h1> </p> </div>
<!-- Tecnical --> <div class="computer_qsnans"> <p>
<h4>
1.What is the full form of HTML
<div>
<input type="radio" name="tech_question_1" id="" value="A">
A) High Transparent Markup Laoyality
<input type="radio" name="tech_question_1" id="" value="B">
B) Hyper Text Markup Language
<input type="radio" name="tech_question_1" id="" value="C">
C) Hyper Text Mechanical Language
<input type="radio" name="tech_question_1" id="" value="D">
D) Hidro Terminate Markup Language
</div>
</h4> </p>
<p>
<h4>
2.What is upcoming android version
<div>
<input type="radio" name="tech_question_2" id="" value="A">
A) Pie
<input type="radio" name="tech_question_2" id="" value="B">
B) N
<input type="radio" name="tech_question_2" id="" value="C">
C) L
<input type="radio" name="tech_question_2" id="" value="D">
D) Q
</div>
</h4> </p> </div> <!-- Political -->
<div id="politics"> <p>
<h1>
Political
</h1> </p> </div>
<div class="politics_qsnans"> <p>
<h4>
1.The present Lok Sabha is the
<div>
<input type="radio" name="poli_question_1" id="" value="A">
A) 14th Lok Sabha
<input type="radio" name="poli_question_1" id="" value="B">
B) 15th Lok Sabha
<input type="radio" name="poli_question_1" id="" value="C">
C) 16th Lok Sabha
<input type="radio" name="poli_question_1" id="" value="D">
D) 17th Lok Sabha
</div>
</h4> </p>
<p>
<h4>
2.The minimum age to qualify for election to the Lok Sabha is
<div>
<input type="radio" name="poli_question_2" id="" value="A">
A) 25 years
<input type="radio" name="poli_question_2" id="" value="B">
B) 21 years
<input type="radio" name="poli_question_2" id="" value="C">
C) 18 years
<input type="radio" name="poli_question_2" id="" value="D">
D) 35 years
</div>
</h4> </p> </div>
<input type="submit" name="Submit" name="sbt" id="sbmt"> </form> </div><!--Main div end-->
</body> </html>
<?php if(isset($_GET['sbt'])) {
$answer1_for_qsn1 = $_GET['tech_question_1']; $answer2_for_qsn2 =
$_GET ['tech_question_2']; $answer3_for_qsn3 = $_GET
['poli_question_1']; $answer4_for_qsn4 = $_GET ['poli_question_2'];
echo $answer1_for_qsn1 ; echo $answer2_for_qsn2 ; echo
$answer3_for_qsn3 ; echo $answer4_for_qsn4 ;
}
?>
You are using $_GET, not $_POST
You are declaring name for button twice, so the first name will be the one that is sent with GET.
Fix:
<input type="submit" name="Submit" id="sbmt">
<?php if(isset($_GET['Submit'])) {
This worked for me when copying all your code and testing it.
EDIT: also send form to self since the if statement is in the same document as form.
<form method="GET" action="">
Make your form method POST and get your data with $_POST["tech_question_1"] instead of $_GET $_POST["sbt"] will not exist
<input type="submit" name="Submit" name="sbt" id="sbmt">
You have declared name twice. Thats why its not working

Adding a user session to a table?

I'm trying to add the user session to a table that is filled in when they complete a form. I would then like to display the form only they created on their home page.
<?php
require_once("/extlib/vdaemon/vdaemon.php");
require_once("./core/config.php");
?>
<html>
<head>
<style type="text/css">
body {
background: #e4e4e4;
}
.form {
width: 600px;
margin: 0 auto;
background: #fff;
padding: 45px;
border: 1px solid #c2c2c2;
}
.error {
color: #AA0000
}
.controlerror {
background-color: #ffffdd;
border: 1px solid #AA0000;
}
.input {
width: 300px;
height: 35px;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="form">
<?php
$msg = $_GET['msg'];
if ( $msg == '1' ) {
echo '<p>Your information was submitted successfully.</p>';
}
?>
<form action="core/process.php" method="post" id="registration" >
<input type="hidden" name="formID" value="Product_Tracker" />
**<input type="hidden" name="id_user" value="$_SESSION['id_user']" />**
<p>Name of product:<input type="text" name="Name of Product" class="input" />
<p>Please select the tests that were done on the product.</p>
<p>In Circuit Test (ICT): Yes: <input type="radio" name="ICT" value="yes" /> No: <input type="radio" name="ICT" value="no" /></p>
<p>Visual Inspection: Yes: <input type="radio" name="Visual Inspection" value="yes" /> No: <input type="radio" name="Visual Inspection" value="no" /></p>
<p>XRAY: Yes: <input type="radio" name="XRAY" value="yes" /> No: <input type="radio" name="XRAY" value="no" /></p>
<p>Automated Optical Inspection (AOI): Yes: <input type="radio" name="AOI" value="yes" /> No: <input type="radio" name="AOI" value="no" /></p>
<!--<p>Checkbox1 <input type="checkbox" name="checkbox" value="checkbox1" /> Checkbox2: <input type="checkbox" name="checkbox" value="checkbox2" /></p>-->
<input type="submit" value="Submit" />
<p>
<a href='access-controlled.php'>Back</a>
</p>
</form>
</div>
</body>
</html>
<?php VDEnd(); ?>
When I look on phpMyAdmin when the form gets created the id_user is $_SESSION['id_user'] and not the name of the user that created it.
You didn't echo the value of $_SESSION['id_user'], Try this
<input type="hidden" name="id_user" value="<?php echo $_SESSION['id_user']"; ?> />

How come my elements are selectable anywhere on their line? How to fix?

I have a form and I can click the text boxes and submit button from anywhere on their line, not just the text box or the submit button itself.
Please help me, this is making me press things accidentally by clicking white space:
<form method="POST" action="">
<label>
<input type="file" name="file" class="custom-file-input">
</label>
<label>
<input name="theirname" type="text" placeholder="What's your name?">
</label>
<label>
<button name="buttonname">Submit</button>
</label>
</form>
It sounds like your labels have been set to display:block- and as they are encapsulating your field controls, clicking anywhere on the row (as the label is 'taking up' the full width) will focus the control.
As such, change your code to change the labels to display:inline-block to stop taking up the 'full width' then using br to seperate each line in your html:
label{
display:inline-block;
}
<form method="POST" action="">
<label>
<input type="file" name="file" class="custom-file-input">
</label><br />
<label>
<input name="theirname" type="text" placeholder="What's your name?">
</label><br />
<label>
<button name="buttonname">Submit</button>
</label>
</form>
Your labels are wrapping your inputs. Typically the label is what would allow you to select an input easier. A checkbox or radio button for instance. You click the text which is wrapped in a label and it will select the input associated with the label:
<form method="POST" action="">
<label for="file">File</label>
<input type="file" name="file" class="custom-file-input" id="file">
<label for="theirname">Name</label>
<input name="theirname" type="text" placeholder="What's your name?" id="theirname">
<label for="submit">Submit</label>
<button name="buttonname">Submit</button>
</form>
EDIT What you are intending (using CSS to float left and such). I am applying it to a div wrapper but you can apply it to the inputs only:
#float-my-boat input,
#float-my-boat button {
float: left;
clear: both;
display: inline-block;
padding: 5px 8px;
font-size: 18px;
color: #666;
margin: 10px 0;
border: 1px solid #CCC;
border-radius: 6px;
box-shadow: inset 1px 1px 4px #888;
}
<div id="float-my-boat">
<form method="POST" action="">
<input type="file" name="file" class="custom-file-input" id="file">
<input name="theirname" type="text" placeholder="What's your name?" id="theirname">
<button name="buttonname">Submit</button>
</form>
</div>

radio button code ok in firefox but don't work in other browsers

The given code works in firefox, but not in any other browser.
Am i going about it wrong by using radio button
I just want 3 buttons but styled as i have already, when the user clicks an option its highlighted
Here is my Code :
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title></title>
</head>
<style>
.radio > input[type=radio]{
display:none;
visibility:hidden;
}
input[type=radio] + label{
cursor:pointer;
}
input[type=radio]:checked + label{
background-color:green;
}
.but1{
background-color:#009;
color:#FFF;
border:none;
border-radius:15px;
width:200px;
height:50px;
font-size:18px;
font-weight:bold;
border-radius:15px;
text-align:center;
padding:8px;
font-family:Verdana, Geneva, sans-serif;
float:left;
margin-left:10px;
}
</style>
</head>
<body>
<form action="Untitled2.php" method="post">
<label class="radio" for="fb1">
<input id="fb1" type="radio" name="time" value="all day" checked />
<label class="but1">Available<br>All day</label>
</label>
<label class="radio" for="fb2">
<input id="fb2" type="radio" name="time" value="between 8-12"/>
<label class="but1">Between<br>8am - 12am</label>
</label>
<label class="radio" for="fb3">
<input id="fb3" type="radio" name="time" value="between 12-4" />
<label class="but1">Between<br>12pm - 4pm</label>
</label>
<input name="submit" type="submit" />
</form>
</body>
</html>
How can i do this ?
Your outer label is redundant, you can instead do it this way:
<input id="fb1" type="radio" name="time" value="all day" checked />
<label class="but1" for="fb1">Available<br>All day</label>
<input id="fb2" type="radio" name="time" value="between 8-12"/>
<label class="but1" for="fb2">Between<br>8am - 12am</label>
<input id="fb3" type="radio" name="time" value="between 12-4" />
<label class="but1" for="fb3">Between<br>12pm - 4pm</label>
Also the css for hiding your radio button was wrong, should be just:
input[type=radio]{
display:none;
}
Fiddle
Updated , Please check the url
Used jQuery as you mentioned http://jsfiddle.net/VE9Tf/4/
<label class="radio" for="fb1">
<input id="fb1" type="radio" name="time" value="all day" checked />
<span class="but1 active">Available<br>All day</span>
</label>
<label class="radio" for="fb2">
<input id="fb2" type="radio" name="time" value="between 8-12"/>
<span class="but1">Between<br>8am - 12am</span>
</label>
<label class="radio" for="fb3">
<input id="fb3" type="radio" name="time" value="between 12-4" />
<span class="but1">Between<br>12pm - 4pm</span>
</label>

Categories