if (!empty($_POST)) not working - php

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'])

Related

How to select another one of radio button?

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.

Keep Radio Checked After Submit

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>

Html Checkboxes Are Not Giving Values To PHP

I am creating an html form that contains multiple checkboxes...
Here is my code
Form
<form enctype="multipart/form-data" action="processstart.php" method="post" class="wpcf7-form">
<div class="form-row">
<span class="label--input-group">Services you require</span>
<span class="wpcf7-form-control-wrap services">
<span class="wpcf7-form-control wpcf7-checkbox check-group-input">
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Branding" /> <span class="wpcf7-list-item-label">Branding</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Design" /> <span class="wpcf7-list-item-label">Design</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Other" /> <span class="wpcf7-list-item-label">Other</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Development" /> <span class="wpcf7-list-item-label">Development</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Illustration" /> <span class="wpcf7-list-item-label">Illustration</span>
</label>
</span>
</span>
</span>
</div>
</form>
Here is processstart.php
<?php
if(isset($_POST['services'])) {
foreach($_POST['services'] as $services) {
echo $services;
}
}
?>
Now nothing is getting echoed...I have tried var_dump(get_defined_vars()); and every other variable is defined except these checkboxes. Not even showing "null" there. What is going wrong?
You have missed to close <form> tag and add submit button
<input type="submit" name="submit" value="submit">
</form>
try i have action on same file but you can change action values will be appear after any checkbox is checked else you can't get checkbox values
On your form not any close <form> tag and not submit form action like button or submit button
<?php if(isset($_POST['services']))
{
foreach($_POST['services'] as $services)
{
echo $services;
}
}?>
<html>
<body>
<form enctype="multipart/form-data" action="" method="post" class="wpcf7-form">
<div class="form-row"><span class="label--input-group">Services you require</span><span class="wpcf7-form-control-wrap services"><span class="wpcf7-form-control wpcf7-checkbox check-group-input"><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Branding" />
<span class="wpcf7-list-item-label">Branding</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Design" />
<span class="wpcf7-list-item-label">Design</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Other" />
<span class="wpcf7-list-item-label">Other</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Development" />
<span class="wpcf7-list-item-label">Development</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Illustration" />
<span class="wpcf7-list-item-label">Illustration</span></label>
</span></span></span></div>
<input type="submit" name="sub">
</form>
</body>
</html>
actually you are not submitting your form. You just specified in 'action'. To achieve your code workable, add a submit button. So that, while clicking it will call processstart.php
<input type="submit">

Can I check a checkbox outside of an HTML input tag?

I am writing a php site that has a form with a series of check boxes. I will be loading an array from a file that I would like to go through and check some of the boxes by default when the form is loaded.
Here is an example:
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" />
</form>
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
Is this possible? What would be the best way to do it.
You should fill your array before the HTML part. And then:
<input type="checkbox" name="option1" value="option1" <?php if (in_array("option1", $array)) { echo 'checked="checked"'; } />
Try this :
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" <?php if(in_array("option1",$array)){?> checked="checked"<?php}?> />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" <?php if(in_array("option2",$array)){?> checked="checked"<?php}?> />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" <?php if(in_array("option3",$array)){?> checked="checked"<?php}?> />
</form>

html5 form not reacting to submit

I've got a simple one-page form, the kind I've written dozens of times, but this behavior is totally new to me. At first, it was submitting on page-load, thus redirecting automatically to the next page in the series. I'm still not sure how I got that to stop, but now it's not doing anything at all when you hit "submit". The page simply sits there.
I've tried stripping out the error-checking scripts, the show/hide script, even jquery itself, then taking the form down to just one input. I tried getting rid of the redirect and just having it output a simple line, then vardump, and still nothing. I hit submit, and no matter what I do, the page just sits there. I've never run into behavior like this before, and firebug et al give me no errors or leads.
If anyone has any ideas at all, no matter how crazy, I'm willing to try. I'm at a loss. Thanks in advance!
<?php
session_start();
include('functions.php');
if ($_POST['submit']) {
$company = $_POST['company'];
$taxid = $_POST['taxid'];
header("location:step2.php");
}
include('head.php'); ?>
<form method="post" name="basic" action="step1.php">
<div class="col70">
<label for="company">Organization/Business name <img src="img/req.jpg" alt="required"></label>
<input type="text" name="company" id="company" value="" />
</div>
<div class="col30">
<label for="taxid">Taxpayer ID# (IEN or SS#) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="taxid" id="taxid" value="" />
</div>
<div class="newcol">
<label for="address">Mailing Address <img src="img/req.jpg" alt="required"></label>
<input type="text" name="address" id="address" value="" />
</div>
<div class="col30 newcol">
<label for="city">City <img src="img/req.jpg" alt="required"></label>
<input type="text" name="city" id="city" value="" />
</div>
<div class="col30">
<label for="state">State <img src="img/req.jpg" alt="required"></label>
<select name="state" id="state" tabindex="<?php echo $tabin; $tabin++; ?>">
<option value="0"></option>
<option value="1">Alabama</option>
</select>
</div>
<div class="col25">
<label for="zipcode">Zip Code <img src="img/req.jpg" alt="required"></label>
<input type="text" name="zipcode" id="zipcode" value="" />
</div>
<fieldset><legend>1. What kind of group/company do you have? <img src="img/req.jpg" alt="required"></legend>
<span id="nfpfp" class="InputGroup">
<label><input name="nfpfp" id="nfpfp_1" type="radio" value="1" />For-profit business.</label>
<label><input name="nfpfp" id="nfpfp_2" type="radio" value="2" />Non-profit 501(c)3 organization.</label>
</span>
</fieldset>
<fieldset><legend>2. How will you use the booth space? Select all that apply. <img src="img/req.jpg" alt="required"></legend>
<span id="type" class="InputGroup">
<label><input name="food" id="type_1" type="checkbox" value="1" />Food sales</label>
<label><input name="retail" id="type_2" type="checkbox" value="2" />Retail sales</label>
<label><input name="activity" id="type_3" type="checkbox" value="3" />Activity</label>
<label><input name="display" id="type_4" type="checkbox" value="4" />Display</label>
<label><input name="other" id="type_5" type="checkbox" value="5" />Other</label>
</span>
</fieldset>
<label for="otherdetails" class="newcol offsides">Enter a short description of your use. (Ex: "BBQ sandwiches", "kite kits", "face painting".) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="otherdetails" id="otherdetails" value="" />
<fieldset><legend>3. Select any/all that apply. Additional questions may appear, if further information is required.</legend>
<span id="additional" class="InputGroup">
<label><input name="raffle" id="raffle_1" type="checkbox" class="switchcheck1" value="1" />I'll be selling raffle tickets and/or holding a raffle at the festival.</label>
<div class="newcol offstate1">
<label for="raffledetails">You'll need written permission from the Exchange Club. Please enter details about the raffle. <img src="img/req.jpg" alt="required"></label>
<textarea name="raffledetails" id="raffledetails" tabindex="<?php echo $tabin; $tabin++; ?>"></textarea>
</div>
<label><input name="trailer" type="checkbox" id="trailer_1" value="1">I'll be bringing a trailer.</label>
<label><input name="outlets" type="checkbox" id="outlets_1" class="switchcheck2" value="1" />I'll require electrical outlets.</label>
<div class="newcol offstate2">
<label for="outletsdetails">How many outlets will you require? <img src="img/req.jpg" alt="required"></label>
<input type="text" name="outletsdetails" id="outletsdetails" />
</div>
</span>
</fieldset>
<input type="button" name="submit" class="a_button" value="submit" />
</form>
The element with name="submit" is of type button and not submit, so it renders a plain button that does nothing (with the intention that you bind some JavaScript to it).
Use type="submit" instead.
Try
<input type="submit" name="submit" class="a_button" value="submit" />
for your submit button.

Categories