Nested radio button - php

I am trying to post a nested radio button with different field name and I have tried all I can, but I have not been successfull.
<form id="w0" class="form-vertical" action="" method="post">
<input type="hidden" name="_csrf" value="">
<label>Choose If you are human</label>
<div class="radio">
<label>
<input type="radio" name="name1" value="1">
<h4> Gender:
<input type="radio" name="male" value="m">Male
<input type="radio" name="gender" value="f">Female
</h4>
</label>
</div>
<hr>
</div>
<div class="help-block"></div>
</div><br>
<div class="form-group">
<button type="submit" class="btn btn success">Submit</button>
</div>
</form>
<?php
if(isset($_POST['name1']) && isset($_POST['gender'])){
$model->name = $_POST['name1'];
$model->gender= $_POST['gender'];
$model->save();
}

Related

How can I pass <input> value into controller in Laravel?

I have a custom input field that acts as a drop-down menu and it works like this:
Each option is an <input> field and I need the value of this input to be passed into the controller. The <input> looks like this:
<!-- Updated -->
<label for="United States">United States</label>
<input type="radio" class="radio" id="United States" name="country" value="United States">
When the user presses the continue button, I want the value of the <input> to be passed to my controller: WelcomeController.
-- Updated --
My controller method looks like this:
public function countrySelect(Request $request)
{
$country = $request->input('country');
dd($country);
}
Here is a basic code structure of the inputs - I think I should be using a form and so have created a route in web.php:
Route::post('/', [\App\Http\Controllers\WelcomeController::class, 'countrySelect'])->name('CS');
And that the input should have the value property:
<!-- Updated -->
<form action="{{ route('CS') }}" method="POST">
<input name="country" value="[COUNTRY Value]">
<button type="submit"> CONTINUE </button>
</form>
The countrySelect method in my controller is empty and so how do I pass the value of the <input> into it? Thanks!
My Full Form:
<form action="{{ route('CS') }}" method="POST">
#csrf
<div class="country-select-container">
<div class="country-align-container">
<div class="CountryInput">
<div class="select-box">
<div class="options-container">
<div class="option">
<label for="United States">United States</label>
<input type="radio" name="country" value="United States">
</div>
<div class="option">
<label for="United Kingdom And Ireland">United Kingdom And Ireland</label>
<input type="radio" class="radio" id="United Kingdom And Ireland" name="country" value="United Kingdom And Ireland">
</div>
<div class="option">
<label for="Philippines">Philippines</label>
<input type="radio" name="country" value="Philippines">
</div>
<div class="option">
<label for="India">India</label>
<input type="radio" name="country" value="India">
</div>
<div class="option">
<label for="Indonesia">Indonesia</label>
<input type="radio" name="country" value="Indonesia">
</div>
<div class="option">
<label for="Malaysia">Malaysia</label>
<input type="radio" name="country" value="Malaysia">
</div>
<div class="option">
<label for="Mexico">Mexico</label>
<input type="radio" name="country" value="Mexico">
</div>
<div class="option">
<label for="Singapore">Singapore</label>
<input type="radio" name="Singapore">
</div>
<div class="option">
<label for="Germany">Germany</label>
<input type="radio" name="country" value="Germany">
</div>
<div class="option">
<label for="Brazil">Brazil</label>
<input type="radio" name="country" value="Brazil">
</div>
<div class="option">
<label for="Canada">Canada</label>
<input type="radio" name="country" value="Canada">
</div>
<div class="option">
<label for="Italy">Italy</label>
<input type="radio" name="country" value="Italy">
</div>
<div class="option">
<label for="Colombia">Colombia</label>
<input type="radio" name="country" value="Colombia">
</div>
<div class="option">
<label for="Australia">Australia</label>
<input type="radio" name="country" value="Australia">
</div>
<div class="option">
<label for="South Africa">South Africa</label>
<input type="radio" name="country" value="South Africa">
</div>
<div class="option">
<label for="France">France</label>
<input type="radio" name="country" value="France">
</div>
<div class="option">
<label for="Pakistan">Pakistan</label>
<input type="radio" name="country" value="Pakistan">
</div>
<div class="option">
<label for="Bangladesh">Bangladesh</label>
<input type="radio" name="country" value="Bangladesh">
</div>
<div class="option">
<label for="Spain">Spain</label>
<input type="radio" name="country" value="Spain">
</div>
<div class="option">
<label for="United Arab Emirates">United Arab Emirates</label>
<input type="radio" name="country" value="United Arab Emirates">
</div>
<div class="option">
<label for="Netherlands">Netherlands</label>
<input type="radio" name="country" value="Netherlands">
</div>
<div class="option">
<label for="Sri Lanka">Sri Lanka</label>
<input type="radio" name="country" value="Sri Lanka">
</div>
<div class="option">
<label for="Russia">Russia</label>
<input type="radio" name="country" value="Russia">
</div>
<div class="option">
<label for="Trinidad & Tobago">Trinidad & Tobago</label>
<input type="radio" name="country" value="Trinidad & Tobago">
</div>
<div class="option">
<label for="Saudi Arabia">Saudi Arabia</label>
<input type="radio" name="country" value="Saudi Arabia">
</div>
<div class="option">
<label for="Thailand">Thailand</label>
<input type="radio" name="country" value="Thailand">
</div>
<div class="option">
<label for="Peru">Peru</label>
<input type="radio" name="country" value="Peru">
</div>
<div class="option">
<label for="New Zealand">New Zealand</label>
<input type="radio" name="country" value="New Zealand">
</div>
<div class="option">
<label for="Vietnam">Vietnam</label>
<input type="radio" name="country" value="Vietnam">
</div>
<div class="option">
<label for="Japan">Japan</label>
<input type="radio" name="country" value="Japan">
</div>
<div class="option">
<label for="Egypt">Egypt</label>
<input type="radio" name="country" value="Egypt">
</div>
<div class="option">
<label for="Argentina">Argentina</label>
<input type="radio" name="country" value="Argentina">
</div>
<div class="option">
<label for="Other">Other...</label>
<input type="radio" name="country" value="Other...">
</div>
</div>
<div class="selected">
Select Country To Continue:
</div>
</div>
</div>
</div>
<div class="guest-action-container">
<div class="go-back-container">
<div class="go-back-btn">
<span class="go-back">
<span class="go-back-icon"></span>
<span class="go-back-text">BACK</span>
</span>
</div>
</div>
<div class="continue-to-site-container">
<div class="continue-to-site-btn">
<button type="submit" class="continue-to-site">
<span class="continue-text">CONTINUE</span>
<span class="continue-icon"></span>
</button>
</div>
</div>
<div class="clearFix"></div>
</div>
</div>
</form>
Here is the JS that creates the dropdown effect:
// country select drop down
$(document).ready(function(){
// country select options
const selected = document.querySelector(".selected");
const optionsContainer = document.querySelector(".options-container");
const optionsList = document.querySelectorAll(".option");
selected.addEventListener("click", () => {
optionsContainer.classList.toggle("active");
});
optionsList.forEach( o => {
o.addEventListener("click", () => {
selected.innerHTML = o.querySelector("label").innerHTML;
optionsContainer.classList.remove("active");
});
});
});
start by changing the code of the form from:
<form action="{{ route('CS') }}" method="POST">
<input value="country">
<button type="submit"> CONTINUE </button>
</form
to
<form action="{{ route('CS') }}" method="POST">
#csrf
<input value="country" name="country">
<button type="submit"> CONTINUE </button>
</form>
i hope that you are seeing the difference. while using a form in laravel you must and the csrf tokem if not i will not work.
After that in your controller just write:
public function handleSubmission(Request $request)
{
$country = $request->input('country');
}
In order to pass the value of an input to your controller you should start by having a "name" parameter in your input, then a POST route, in your web.php file, that calls the method in the controller that should get the values. Finally use request('name-of-input') and that should return the value.
Example:
view:
<form action="/user" class="admin-form" method="POST">
#csrf
<label for="form-name">Name: </label>
<input type="text" id="form-name" name="name" required>
<label for="form-email">Email: </label>
<input type="email" id="form-email" name="email" required>
<input id="form-submit" type="submit" value="Sign up!">
</form>
web.php:
Route::post('/user', 'App\Http\Controllers\UserController#store');
controller:
public function store(Request $request)
{
$user = new User();
$user->name = request('name');
$user->email = request('email');
$user->save();
return redirect('/user/create');
}
You need to include the name attribute in the input field like this:
<form action="{{ route('CS') }}" method="POST">
<input name="country" value="country">
<button type="submit"> CONTINUE </button>
</form
Then in the controller action you can reference the name field like this:
public function handleSubmission(Request $request)
{
$country = $request->input('country');
}

Bootstrap form does not send file name

I have wrote a form in bannervipsites.php :
<form role="form" action="index.php" method ="post">
<div class="form-group">
<label>نام بنر :</label>
<input class="form-control" name="sname" placeholder="نام فارسی یا لاتین بنر">
</div>
<div class="form-group">
<label>URL : </label>
<input class="form-control" name="surl" placeholder="http://">
</div>
<div class="form-group">
<label>Upload banner :</label>
<input type="file" name='upfile' id='upfile' class="filestyle" data-buttonName="btn-primary" data-buttonBefore="true" data-icon="false" >
<input type='hidden' name='bform' value='file'>
<input type='hidden' name='MAX_FILE_SIZE' value='100720'>
</div>
<?php
if ($acctype != 1) {
?>
<div class="form-group">
<label>حداکثر بازدید در ساعت : (عدد 0 یعنی نامحدود)</label>
<input class="form-control" name="scph" placeholder="سقف بازدید در ساعت از چه عددی فراتر نرود؟">
</div>
<?php
}
?>
<div class="form-group">
<label>روی مربع کلیک کنید : </label>
<div class="g-recaptcha"></div>
</div>
<input type="hidden" name="fform" value="bannervip">
<input type="hidden" name="sid" value="0">
<input id="submit" name="submit" value="اضافه کردن" class="btn btn-success" type="submit">
<button type="button" class="btn btn-default" data-dismiss="modal">بستن</button>
</form>
and then post it to index.php
but upfile (the name of uploaded file) does not send.
I can not get
echo $_FILES['upfile']['tmp_name'];
in index.php
what is the problem? it's very odd for me.
it was working in last template .(index.php is ok i'm sure)
what is wrong with bootstrap form
Add
enctype= "multipart/form-data"
to your form.

PHP: Bootstrap Radio button value not showing

I have a bootstrap form with 3 radio buttons. I am saving the value into a variable so that I can use to save into database and email.
HTML:
<form class="form-horizontal form-validate" id="signup-form" method="post">
<input type="hidden" name="signupForm" />
<div class="control-group">
<label class="control-label">Seed program</label>
<div class="controls">
<input type="radio" name="signup" value="Seed Program" checked="checked"/>
</div>
</div>
<div class="control-group">
<label class="control-label">Gift Wrap Program</label>
<div class="controls">
<input type="radio" name="signup" value="Gift Wrap" />
</div>
</div>
<div class="control-group">
<label class="control-label">Sign up for both</label>
<div class="controls">
<input type="radio" name="signup" value="Both" />
</div>
</div>
<input type="submit" class="btn btn-large btn-block btn-success" value="Submit" name="submit">
</form>
PHP:
if(isset($_POST['signupForm'])){
if(isset($_POST['signup'])) {
$signup = $_POST["signup"];
}
else{
$signup = "Nothing was selected";
}
}
The Problem:
The Problem is that I can only get the value of the first radio button which has the "checked" attribute. If I select any of the other two, I wont get anything and the value shows empty.
Any help is appreciated.
Thanks,
try using
<input type="radio" name="signup" value="Seed Program" checked />
and not
<input type="radio" name="signup" value="Seed Program" checked="checked"/>

$_POST not being read

i have a form which inserts book information into the database. however it is not reading the $_POST attribute.
book.php:
<form action="books_manage.php" id="addbook_form" method="post">
<div id="ab_wrapper">
<div id="ab_leftcolumn">
<div id="bookinfo">
<fieldset>
<legend>Book Details</legend>
<div class="field">
<label>Book ID</label>
<input type="text" name="bid" id="bid"/>
</div>
<div class="field">
<label>Name</label>
<input type="text" name="bname" id="bname"/>
</div>
<div class="field">
<label>Author</label>
<input type="text" name="bauthor" id="bauthor"/>
</div>
<div class="field">
<label>Info</label>
<textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
</div>
<div class="field">
<label>Date Added</label>
<input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
</div>
<div class="field">
<label>Date Updated</label>
<input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
</div>
<div>
<input type="hidden" name="action" value="save">
<input type="submit" value="Save">
<input type="button" id="addcontent" value="Add Content">
<input type="reset" value="Reset">
</div>
</fieldset>
</div>
</div>
<div id="ab_rightcolumn">
<div id="bookcontents">
<fieldset>
<legend>Book Content</legend>
<div class="field">
<label>Chapter</label>
<input type="text" id="bchapter" name="bchapter"/>
</div>
<div class="field">
<label>Sub-Chapter</label>
<input type="text" id="bsubchapter" name="bsubchapter"/>
</div>
<div class="field">
<label>Content</label>
<textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
</div>
<br />
<div>
<input type="hidden" name="action" value="addnext">
<input type="submit" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
</div>
</fieldset>
</div>
</div>
</div>
</form>
books_manage.php:
<?php
if (isset($_POST['action']) && $_POST['action'] == 'save')
{
echo "You clicked the save button";
}
else {
echo "Hello. The date is " . date("D d M Y") ;
}
?>
the output:
Hello. The date is Thu 08 Jul 2010
it seems it isn't reading the value of the hidden button. it should display "You clicked the save button". Am I missing something?
First of all, multiple <input>s in the same <form> with the same name attribute isn't going to get you the behavior you're looking for.
Instead, you need to provide a name to the submit buttons, and then you can check which button was pressed:
<input type="submit" name="save" value="Add Content">
<input type="submit" name="done" value="No more content">
<?php
if(isset($_POST['save'])) {
echo "saved";
} else if(isset($_POST['done'])) {
echo "done";
}
?>
See comment below by Lèse majesté to learn how the HTML working group effed this one up.
You have two inputs with the name "action" in the same form. Make sure your form field names are unique.
Don't forget you can organise your names using this syntax -
<input name="form1['name']" value="".....
<input name="form2['name']" ..... etc
Then access these variables like this:
$_POST['form1']['name']...
Very useful!
Its becuase you have defined action 3 times
<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">
Do the followin on your books_manage.php
echo "<pre>";
print_r($_POST);
echo "</pre>";
You will see where you are going wrong.
<input type="hidden" name="action" value="addnext">
<input type="submit" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
you have two inputs with name "action". the action you get is probably "Done", not "save"
You have multiple inputs named action you will get
<input type="hidden" name="action" value="save">
<input type="hidden" name="action" value="addnext">
<input type="submit" name="action" value="Done">
You need to remove the hidden variables and change the name of your first 'submit' to 'action'
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Done">
You need to name your submit button "action" and use the value of that button to determine the action. Your code basically has two action form values and the last one is what takes precedence.
<form action="books_manage.php" id="addbook_form" method="post">
<div id="ab_wrapper">
<div id="ab_leftcolumn">
<div id="bookinfo">
<fieldset>
<legend>Book Details</legend>
<div class="field">
<label>Book ID</label>
<input type="text" name="bid" id="bid"/>
</div>
<div class="field">
<label>Name</label>
<input type="text" name="bname" id="bname"/>
</div>
<div class="field">
<label>Author</label>
<input type="text" name="bauthor" id="bauthor"/>
</div>
<div class="field">
<label>Info</label>
<textarea name="binfo" id="binfo" cols="5" rows="5" ></textarea>
</div>
<div class="field">
<label>Date Added</label>
<input type="text" value="<?php echo date('D d M Y')?>" name="bdateadd" id="bdateadd"/>
</div>
<div class="field">
<label>Date Updated</label>
<input type="text" value="<?php echo date("D d M Y")?>" name="bdateupd" id="bdateupd"/>
</div>
<div>
<input type="submit" name="action" value="Save">
<input type="button" id="addcontent" value="Add Content">
<input type="reset" value="Reset">
</div>
</fieldset>
</div>
</div>
<div id="ab_rightcolumn">
<div id="bookcontents">
<fieldset>
<legend>Book Content</legend>
<div class="field">
<label>Chapter</label>
<input type="text" id="bchapter" name="bchapter"/>
</div>
<div class="field">
<label>Sub-Chapter</label>
<input type="text" id="bsubchapter" name="bsubchapter"/>
</div>
<div class="field">
<label>Content</label>
<textarea id="bcontent" name="bcontent" rows="6" cols="8"></textarea>
</div>
<br />
<div>
<input type="submit" name="action" value="Save and Add Next Chapter">
<input type="submit" name="action" value="Done">
</div>
</fieldset>
</div>
</div>
</div>
<?php
if (isset($_POST['action']) && $_POST['action'] == 'Save')
{
echo "You clicked the save button";
}
else if (isset($_POST['action']) && $_POST['action'] == 'Save and Add Next Chapter')
{
echo 'You clicked the "Save and Add Next Chapter" button';
}
else if (isset($_POST['action']) && $_POST['action'] == 'Done')
{
echo 'You clicked the done button';
}
else
{
echo "Hello. The date is " . date("D d M Y") ;
}
?>

Can't choose between Radio buttons

I have the following form code but I cannot choose between these for choices.
I can't figure where is the problem. all i know about radio buttons is that they need to have the same name
<div class="controls">
<input type="radio" name="pub_place" id="1" value="1" >1
<input type="radio" name="pub_place" id="2" value="2" >2
<input type="radio" name="pub_place" id="3" value="3" >3
<input type="radio" name="pub_place" id="4" value="3" >4
</div>
this is all the view
<form class="form-horizontal" action="{{ url('/save-publicity')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<div class="control-group">
<label class="control-label" for="date01">Publicity Name</label>
<div class="controls">
<input type="text" class="input-xlarge" name="pub_name" required="">
</div>
</div>
<div class="control-group">
<label class="control-label" for="date01">website Link</label>
<div class="controls">
<input type="text" class="input-xlarge" name="pub_link" required="">
</div>
</div>
<div class="control-group">
<label class="control-label" for="fileInput">Publicity Image</label>
<div class="controls">
<input class="input-file uniform_on" name="pub_image" id="pub_image" type="file" required="">
</div>
</div>
<div class="control-group hidden-phone">
<label class="control-label" for="textarea2">Publication status </label>
<div class="controls">
<input type="checkbox" name="pub_status" value="1">
</div>
</div>
<div class="control-group hidden-phone">
<label class="control-label" for="textarea2">Pub Place </label>
<div class="controls">
<input type="radio" name="pub_place" id="1" value="1" >1
<input type="radio" name="pub_place" id="2" value="2" >2
<input type="radio" name="pub_place" id="3" value="3" >3
<input type="radio" name="pub_place" id="4" value="4" >4
</div>
<p>NB: <strong>Place 1 dimension:</strong> 1170x150 |
<strong>Place 2 dimension:</strong> 850x100 |
<strong>Place 3 and 4 dimension:</strong> 270x329</p>
<img style="width:300px"src="backend/brunch.jpg" alt="" />
<div class="form-actions">
<button type="submit" class="btn btn-primary">Add Publicity</button>
<button type="reset" class="btn">Cancel</button>
</div>
</div>
</fieldset>
</form>
they keep giving me NULL on the column pub_place on my database

Categories