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"/>
Related
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();
}
I am a bit new to PHP and I'm having a bit of difficulties here and there. I am developing a form and wish to display an error box if any of the fields are empty when the 'Submit' Button is pressed. I've tried the following code but the echo is still not appearing. Any suggestions ?
Form Code:
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="$_POST" action="searchEmployee.php">
<br style="clear:both">
<h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
</div>
<div class="form-group">
<label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label>
<input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div>
<div class="form-group">
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
</div>
</form>
</div>
</div>
</div>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$required = array('name', 'surname', 'ID', 'visitorCard');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
}
}
?>
You are using method as $_POST in your form attribute,
It should be only POST.
So, replace your form line with,
<form role="form" method="POST" action="searchEmployee.php">
and also, change Submit button line to,
<input type="submit" name="submit" value="Sign In Visitor" class="btn btn-primary pull-right" />
Here are few mistakes:
You are using method as $_POST in your form attribute, It should be only POST.
Your form will not submit because your button is not a submit type. it should
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor" />
Or
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
You need to make 2 change as below
Change button type like this
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
Change Form method
<form role="form" method="POST" action="searchEmployee.php">
Change this
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
To
<input type="button" id="submit" name="submit" value="submit" class="btn btn-primary pull-right" />Sign In Visitor
and Also
this
<form role="form" method="$_POST" action="searchEmployee.php">
To
<form role="form" method="POST" action="searchEmployee.php">
How will you get POST['submit'] value when you have not even set, that means your if code won't execute it and so it won't echo or alert it.
For Best practice of debugging always try to do this whenever such instances occurs while coding.
print_r($_POST);
This will show you an array of POST variables
change $_POST to POST and put <input type="submit" instead of <input type = "button"
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="post" action="searchEmployee.php"> <br style="clear:both"> <h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3> <div class="form-group"> <label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required> </div>
<div class="form-group"> <label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required> </div> <div class="form-group"> <label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label> <input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div> <div class="form-group">
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
</div> </form> </div>
</div> </div>
Php Code:::searchEmployee.php
<?php if (isset($_POST['submit'])) { $required = array('name', 'surname', 'idCard', 'cardNumber');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) { if (!isset($_POST[$field])) {
$error = true;
} }
if ($error) {
echo "All fields are required."; } }
?>
First think change button type="submit".
Second think change PHP array
$required = array('name', 'surname', 'idCard', 'cardNumber');
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.
<form action="actions/add_cat.php" method="post" id="rtf" name="">
<input type="text" name="cat_title" id="cat_title" required="required" placeholder="Category Title"/>
<br /><br />
<button type="button" onclick="button_click('#d31b26');" value="d31b26" name="cat_color"><div class="redSelect"></div></button>
<button type="button" onclick="button_click('#f9c04c');" name="cat_color" value="#f9c04c"><div class="yellowSelect"></div></button>
<button type="button" onclick="button_click('#72bce9');" name="cat_color" value="#72bce9"><div class="blueSelect"></div></button>
<button type="button" onclick="button_click('#ec9292');" name="cat_color" value="#ec9292"><div class="pinkSelect"></div></button>
<button type="button" onclick="button_click('#b7d04e');" name="cat_color" value="#b7d04e"><div class="greenSelect"></div></button>
<div class="indexBox">
<div class="indexBoxHeader" id="box">
<i class="fa fa-question-circle" style="font-size: 2em;"></i></div>
<div class="indexBoxFooter">
<div class='printchatbox' id='printchatbox'></div>
</div>
</div>
<br><br>
<input onclick="formsubmit()" type="submit" value="Create Category" name="submit"/>
</form>
If i test using a text input and using the cat_color name it will post that entry but when using the above it does not seem to to take the item I am selecting?
Here is the post query incase you need to see it:
$sql = "INSERT INTO cat_list (cat_title, cat_color) VALUES ('".$_POST["cat_title"]."', '".$_POST["cat_color"]."')";
As I can't comment.
Try to have <radio> buttons instead of normal buttons. So the php/form would understand it is and option.
Example:
<form .. >
..
<input type="radio" name="cat_color" value="#d31b26"><div class="redSelect"></div><br>
<input type="radio" name="cat_color" value="#f9c04c"><div class="yellowSelect"></div><br>
..
</form>
EDIT
<form action="actions/add_cat.php" method="post" id="rtf" name="">
<input type="text" name="cat_title" id="cat_title" required="required" placeholder="Category Title"/>
<br /><br />
<!-- Radio Buttons With customized color class -->
<input type="radio" name="cat_color" value="#d31b26" class="redSelect"><br>
<input type="radio" name="cat_color" value="#f9c04c" class="yellowSelect"><br>
<input type="radio" name="cat_color" value="#72bce9" class="blueSelect"><br>
<input type="radio" name="cat_color" value="#ec9292" class="yellowSelect"><br>
<input type="radio" name="cat_color" value="#b7d04e" class="greenSelect"><br>
<div class="indexBox">
<div class="indexBoxHeader" id="box">
<i class="fa fa-question-circle" style="font-size: 2em;"></i></div>
<div class="indexBoxFooter">
<div class='printchatbox' id='printchatbox'></div>
</div>
</div>
<br><br>
<input onclick="formsubmit()" type="submit" value="Create Category" name="submit"/>
</form>
So in the in the end I just wrapped a hidden field inside the div for my colour picker like so:
<div class="redSelect" onclick="button_click('#d31b26');"><input type="hidden" name="cat_color" value="#f9c04c" ></div>
<div class="yellowSelect" onclick="button_click('#f9c04c');" ><input type="hidden" name="cat_color" value="#f9c04c" ></div>
<div class="blueSelect" onclick="button_click('#72bce9');"><input type="hidden" name="cat_color" value="#72bce9" ></div>
<div class="pinkSelect" onclick="button_click('#ec9292');"><input type="hidden" name="cat_color" value="#ec9292"></div>
<div class="greenSelect" onclick="button_click('#b7d04e');"><input type="hidden" name="cat_color" value="#b7d04e" ></div>
works a treat and posts out the value of the hidden field
With me, happened that my form had so so many inputs that it simply didn't send the last ones (the button and some others). In some way, there is a "limit" on how much data you may send by POST.
Then, I put a hidden input in the upper part of the form with the same name of button and it worked 👍🏻.
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") ;
}
?>