How do i keep the radio checked after user submitted the form?
User will stay on the same page after submit. Is there any other way except AJAX? I am not familiar with AJAX, prefer PHP.
<form action="submit.php" method="POST">
<div id="radioboomboomboom">
<input type="radio" name="branch" value="[KL]" required> KL <br>
<input type="radio" name="branch" value="[JB]" required> JB <br>
<input type="radio" name="branch" value="[PG]" required> Penang <br>
<input type="radio" name="branch" value="[MLK]" required> Melacca</div>
<br>
<input type="submit" name= "submit" id="submit" value="Submit"/>
</form>
You need to check POST data and based on that add checked attribute to the corresponding radio button.
Do like this:-
<form action="submit.php" method="POST">
<div id="radioboomboomboom">
<input type="radio" name="branch" value="[KL]" <?php if(isset($_POST['branch']) && $_POST['branch'] =='[KL]' ){echo "checked";}?> required> KL <br>
<input type="radio" name="branch" value="[JB]" <?php if(isset($_POST['branch']) && $_POST['branch'] =='[JB]' ){echo "checked";}?> required> JB <br>
<input type="radio" name="branch" value="[PG]" <?php if(isset($_POST['branch']) && $_POST['branch'] =='[PG]' ){echo "checked";}?> required> Penang <br>
<input type="radio" name="branch" value="[MLK]" <?php if(isset($_POST['branch']) && $_POST['branch'] =='[MLK]' ){echo "checked";}?> required> Melacca
</div>
<br>
<input type="submit" name= "submit" id="submit" value="Submit"/>
</form>
Related
<form action="confirm.php" method="post" name="">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
<br>
<button type="submit" class="">Submit</button>
</form>
having problem with the radio buttons.
And on the confirm page I have used foreach loop. How do i also get the values for "f_status" ?
See first of all its an interesting question but unfortunately, the fact is HTML can't understand the field without different names if they are in same form.
so the only way to achieve your goal is to put all of them in three different form tags and then u can name all of the same i.e. f_hobby[]
Also, you need to add a single button to submit all three of them. To achieve this u can use onsubmit() or onclick() function.
<form action="confirm.php" method="post" name="" id="form1">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form2">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form3">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<button type="submit" class="" onclick="submitForms()">Submit</button>
<script>
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
document.getElementById("form3").submit();
alert("gajab");
}
</script>
I have given the forms an id to submit it using a single button, u can also use class instead. I am sure this will solve your problem.
I made radio buttons and all radio buttons are selected, where I try my code.
How to choose just one radio button in sintaks laravel?
This is My View Page :
<div class="form-group">
<b>Paket</b>
<br/>
<fieldset>
<input type="checkbox" name="delux" id="delux" value="d"> <label for="">Paket Delux </label>
<input type="checkbox" name="paket1" id="p1" value="p1"> <label for="">Paket 1</label>
<input type="checkbox" name="paket2" id="p2" value="p2"> <label for="">Paket 2</label>
</fieldset>
</div>
<div class="form-group">
<b>Jenis Pembayaran</b>
<br/>
<fieldset>
<form id="form_radio" name="form_radio">
<input type="radio" value="tunai" name="tunai" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="nontunai" id="rd2"> <label for="">non tunai</label>
</fieldset>
</div>
<input type="submit" value="Upload" class="btn btn-primary">
</form>
And this is My controller :
public function input()
{
$jenis = JenisMkn::select('id_jenis','jenis_makanan')->get();
return view('upload_gambar',['jenis'=>$jenis]);
}
public function proses(Request $request)
{
$cek = Gambar::get('checkbox');
echo $cek;
$radio = Gambar::get('radio');
echo $radio;
What the fault in my code?
Any help? Thank you.
Simply give them the same name, cek this code:
<input type="radio" value="tunai" name="transaksi" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="transaksi" id="rd2"> <label for="">non tunai</label>
You can rename the name input with your name.
If you want only one radio button to get selected then you must specify same value for name property. for example:
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
Hope it helped.
the variable $res stock just the last submit input clicked. it must stock all submit values which i clicked in.
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="one" value="2">
<input type="submit" name="one" value="3">
</p>
<p>
<input type="submit" name="one" value="4">
<input type="submit" name="one" value="5">
<input type="submit" name="one" value="6">
</p>
<p>
<input type="submit" name="one" value="7">
<input type="submit" name="one" value="8">
<input type="submit" name="one" value="9">
</p>
<p>
<input type="submit" name="operation" value="+">
<input type="submit" name="one" value="0">
<input type="submit" name="operation" value="-">
</p>
</form>
php code :
<?php
$res="";
if(isset($_POST['one']))
{
$val = $_POST['one'];
$res.=$val;
echo $res;
}
?>
</body>
</html>
concatenation doesn't work on input submit, $res stock just a value of one input.
if u want to concatenate something it goes like
$res = "something";
$res .= $_POST['one']
.= concatenates strings .. what you are doing is that you are assigning the value to the string, so whatever is inside will be replaced by the $_POST['one'] value
from what i can see from your html, your designing a calculator. so you want to enter each number assigned to a button into the text field. try this:
html file
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="two" value="2">
<input type="submit" name="three" value="3">
</p>
<p>
<input type="submit" name="four" value="4">
<input type="submit" name="five" value="5">
<input type="submit" name="six" value="6">
</p>
<p>
<input type="submit" name="seven" value="7">
<input type="submit" name="eight" value="8">
<input type="submit" name="nine" value="9">
</p>
<p>
<input type="submit" name="plus" value="+">
<input type="submit" name="zero" value="0">
<input type="submit" name="minus" value="-">
</p>
</form>
function.php
<?php
if (isset($_POST['one'])) {
$num1 .= $_POST['one'];
echo $num1;
}
if (isset($_POST['two'])) {
$num2 .= $_POST['two'];
echo $num2;
etc...........
}
?>
First my code:
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post">
<P>
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</P>
</FORM>
';
?>
So if i run that on my xampp-Server, it shows a "Hello" and the Form in a new line.
What must I do that all this is written in one line?
Thanks
You need to remove the <p> element and display the form inline.
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post" style="display:inline">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
Remove the paragraph break '<P>'
You should really use a heredoc for this type of HTML output in PHP. Technically, the <p> tag should be around the Hello, not the form. You're looking for something like:
<?php
echo <<<EOT
Hello
<FORM ACTION="uebung3.php" METHOD="post">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
EOT;
?>
Even by removing <p> tags, your hello will still appear on a seperate line. This is because it is outside your <form> tag.
Put it inside <form> like this, while removing the <p> tags:
<?php
echo '<FORM ACTION="uebung3.php" METHOD="post">Hello
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
I have a php form (code to follow) with a submit button that runs JSON-events.php as its action (method = POST). In the JSON-events code I am testing whether the form has been submitted using if (!empty($_POST)). My issue is that the JSON-events code doesnt seem to be recognising $_POST.
Heres the form side code section.
<div class="simple_overlay" id="searchform">
<form id="myform" class = "cols" action="json-events.php" method="post" >
<h3>Search</h3>
<p>
<label>address/postcode *</label>
<input type="address" id="searchaddress" />
</p>
<p>
<label for="amount">maximum distance:</label>
<input type="text" id="amount" value="5 miles" style=" border:0; color:#43a8c4; font-weight:bold;" />
</p>
<div id="slider-range-min" style="width:130px; margin-left:4px; margin-bottom:20px"></div>
<p id="terms">
<label>Category 1</label>
<input name="cat1" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 2</label>
<input name="cat2" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 3</label>
<input name="cat3" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 4</label>
<input name="cat4" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 5</label>
<input name="cat5" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 6</label>
<input name="cat6" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 7</label>
<input name="cat7" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 8</label>
<input name="cat8" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 9</label>
<input name="cat9" type="checkbox" value="1" checked="checked"/>
</p>
<p id="terms">
<label>Category 10</label>
<input name="cat10" type="checkbox" value="1" checked="checked"/>
</p>
<p>
<input type="hidden" name="searchlat" id="searchlat"/>
</p>
<p>
<input type="hidden" name="searchlong" id="searchlong" />
</p>
<input type="submit" name="search" id="search"/>
<input type="button" value="Check Address" onclick="codeAddressSearch()"/>
<button type="reset">Reset</button>
</form>
</div>
and here is the top part of the JSON...
if (!empty($_POST)) {
any help very much appreciated
empty() will work if $_POST does not have any values (Empty array), but in your case when you submit without any values still you get an array like below and it's not empty:
Array
(
[searchlat] =>
[searchlong] =>
[search] => Submit Query
)
empty() will return true only if $_POST returns
Array (
)
But it's not going to happen as every form will have one Sumbit button.
Just use
if($_POST) {
//php code
}
It will solve your problem.
Learn more about empty() by visiting http://php.net/manual/en/function.empty.php
Do not use
if (!empty($_POST)) {
to check if there is a post done use this:
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
Conditions that you can use with $_POST
if(count($_POST)>0){
//...
}
if(isset($_POST['field_name'])){
//...
}
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
//..
}
Try this
if(!empty($_POST['search']) && isset($_POST['search']))
much better to check for an actual value in the $_POST array. Eg.
if (!empty($_POST['search']))
Using empty() will fail if value 0 is posted
So it is better to use
isset($_POST['FIELD_NAME'])