get checkbox value in to a variable - php

This is my checkbox
<input name="interests2" type="checkbox" value="double-deep-racks" />
This is how I am trying to get that value in to a variable
$int = $_POST['interests2'];
Can you please tell me what i am doing wrong. I cant get the values I just get blank.

Try
$int = $_POST['interests2'];
If you are trying to set multiple checkboxes you can do something like,
// Your html
<input type="checkbox" name="interests[]" value="This is i">
<input type="checkbox" name="interests[]" value="Another i value">
// php
$email = "Further Information In: \n";
foreach($_POST['interests'] as $i)
$email .= $i . "\n";

The name of your checkbox is interests2. You must get the value by that name like this:
$int = $_POST['interests2'];

The name element must match what you are looking for. In your input field the name is interests2 but you are looking for interests (missing "2").
Also, you may possibly need to look in $_GET instead of $_POST, depending on the form or the AJAX method you are using (you didn't post that portion of your code).

Related

How can I put a POST inside another POST in PHP

I am trying to take the contents of a form and i want to display them using PHP. The way the form is structured is the following:
There is a list of checkboxes, which when they are clicked, a JavaScript function will create another <input> tag. The JS then sets the name of the new input to <input name="value of checkbox">.
What I need to do is get my PHP to take the value of the <input> I created.
Here is my HTML of the checkboxes.
<input name="product[]" value="value of Check 1" id="id-check-1" type="checkbox" onclick="id-check-1"/><input name="product[]" value="value of Check 2" id="id-check-2" type="checkbox" onclick="id-check-2"/><input name="product[]" value="value of Check 3" id="id-check-3" type="checkbox" onclick="id-check-3"/>
Here is my HTML of the inputs created by the JS.
<input name="value of Check 1" type="text"/><input name="value of Check 2" type="text"/><input name="value of Check 3" type="text"/>
PHP:
$aProduct = $_POST['product];
$product1 = $aProduct[0];
$product2 = $aProduct[1];
$product3 = $aProduct[2];
$input1 = $_POST[$product1];
$input2 = $_POST[$product3];
$input3 = $_POST[$product3];
I tried using this PHP to obtain the value inputted to the crated inputs. This shouldwork, since the name of the inputs = the value of the checkboxes, so in short, this is what I've done:$_POST[$_POST['product']]
Unfortunately, it doesnt work and the variables $input1, $input2 & $input3 dont return any value. So I'd like to know: A) Can i put a _POST within a _POST? and B) What can I do to make my code work?
First of alll the $_POST Variable is one Super Global in PHP i dont think it makes any sense to use $_POST[$_POST...
http://php.net/manual/en/language.variables.superglobals.php
Also i dont think you have to create extra inputs just use $_POST['product'] (array of values which are selected)
try var_dump($_POST['product']) to see whats stored in it
If I understand correctly otherwise guide me in the right direction.
First: You have given the name product[]. Then it should be $_POST['product[]'] which I don't know if that is possible.
Second: If you are writing the value of the checkbox to an input field than why not just check the checkboxes by name. For example:
<input name="check1" value="checkbox 1 value" type="text"/>
<input name="check2" value="checkbox 2 value" type="text"/>
<input name="check3" value="checkbox 3 value" type="text"/>
then in php do:
$check1 = isset($_POST['check1']) ? $_POST['check1'] : "";
This way if the checkbutton is checked you get the value of check1 and otherwise it's empty.
Potentially, you are having issues because you can't have whitespace in the name attribute.
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
https://www.w3.org/TR/html401/types.html#type-name

Array to String Conversion error in Codeigniter

Well I am Stuck some where here in Array conversion:
My Controller:
$username = $this->input->post('FirstName');
$countryval= $this->input->post('country');
var_dump($countryval);
My View:
<input type="text" name="FirstName" id="Name" >
<?php
foreach ($countryDetails as $row )
{
echo '<input id="country" type="checkbox" name="country[]" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
}
?>
<script>
$('input.unique').click(function() {
$('input.unique:checked').not(this).removeAttr('checked');
});
</script>
I am getting the value from checkbox in an array and I need to pass that value as a string further.
I am not able to convert value from array to string i.e if I var_dump($countryval) I get value in array ! Please Help. Thank you
That is because you have used "country[]" as the name in input field
Please try this incase you need a single value to be returned (Edit made here coz I put type="checkbox" instead of "radio".. I have corrected it) Trust radios for single values.
echo '<input id="country" type="radio" name="country" class="unique" value="'.$row->country_id.'">'.$row->country_name.'<br/>';
hope that helps
You need "country[ ]" as name only if it has multiple values. Eg., For a multiple select
<select name="country[]" multiple>
<option></option> <!-- Your options go here-->
</select>
You can use input-checkbox too.. But with your question, I believe you need just a single value to be returned.
Ok.. Now From Your comments I kind of get what you want. Here is my suggestion
1) When you are having multiple checkboxes with the same name, the values are sent as an array.
2) If you would need a single value at a time and definitely need a checkbox, You can make a radio look like a checkbox like this.
3) If you really want to proceed with your javascript allowing only one checkbox at a time, you can do this.
// Load the 'array' helper
$this->load->helper('array');
// Use the 'element' function to return an element from the array
$countryval = element('0', $this->input->post('country')); //should work
Have a try..!!

How $_POST variable/operation is managed in PHP?

When I do something like:
foreach($_POST as $post_key => $post_value){
/* Any code here*/
}
So, something like:
$varSomething = $_POST['anything'];
$varSomethingElse = $_POST['somethingElse'];
Is it possible? When I catch a $_POST[' '], isn't that variable already consumed?
The main reason why I would do this is because after a form submission, I want to check wether some items of some type got certain value or not.
Is there aything else more appropiate?
Firstly the html code don't use variable types, for example, if you have
<input id="check" type="checkbox" />
without a established value, after that you have echo $_POST['chek'], you could think that the result would be a boolean value (false or true), but the correct result will be "on" or "off", you can coding this case. Also, if you want to know the type of your data, you can use regular expression on server side, for example:
<input type="text" id="number" value="1350" />
.....
PHP code
$data = $_POST['number'];
$regularExpression = "/^\d{1,10}$/";
if (preg_match($regularExpression, $data)) {
echo "Is numeric";
}
Good lucky.
if you don't know what is the name of element which is sending the data. the first method is ohk . but if know the name like password or username you can use second one
in html
<input type="password" name ="password" />
in php
$pass_recvd=$_POST['password'];
there is no way to check the type i.e. text/password/checkbox/select etc. you have to do it on client side BEST WAY IS USING Jquery
if you wanna check whether a variable is set or not simple check by using isset method
if( isset($_POST['someVariableName'])) {}else{}

counting number of checkboxes check in email

Here is my php code:
$tasks = ' ';
$help = $_POST['help'];
if(empty($help))
{
$tasks = "None selected.";
}
else
{
$N = count($help);
$tasks = $N;
}
And the HTML is:
<input type="checkbox" name="help" value="sign"> //with several inputs with different values
On the form submit, it emails and outputs everything appropriately except the count of the array. It outputs the $tasks variable at the end of the email always as 1, except when no check boxes are selected. Any combination of selecting checkboxes (1-6) ends up with an array of 1 length. Anyone know why? Thanks!
You'll need to make the checkboxes an array. Change the name to:
<input type="checkbox" name="help[]" value="sign">
You should change your HTML code to:
<input type="checkbox" name="help[]" value="sign">
so that help will be an array. If you only use help, $_POST['help'] will only contain the last value.
you have to rename fields name="help[]" so it can be parsed as array.

Get all variables sent with POST?

I need to insert all variables sent with post, they were checkboxes each representing a user.
If I use GET I get something like this:
?19=on&25=on&30=on
I need to insert the variables in the database.
How do I get all variables sent with POST? As an array or values separated with comas or something?
The variable $_POST is automatically populated.
Try var_dump($_POST); to see the contents.
You can access individual values like this: echo $_POST["name"];
This, of course, assumes your form is using the typical form encoding (i.e. enctype=”multipart/form-data”
If your post data is in another format (e.g. JSON or XML, you can do something like this:
$post = file_get_contents('php://input');
and $post will contain the raw data.
Assuming you're using the standard $_POST variable, you can test if a checkbox is checked like this:
if(isset($_POST['myCheckbox']) && $_POST['myCheckbox'] == 'Yes')
{
...
}
If you have an array of checkboxes (e.g.
<form action="myscript.php" method="post">
<input type="checkbox" name="myCheckbox[]" value="A" />val1<br />
<input type="checkbox" name="myCheckbox[]" value="B" />val2<br />
<input type="checkbox" name="myCheckbox[]" value="C" />val3<br />
<input type="checkbox" name="myCheckbox[]" value="D" />val4<br />
<input type="checkbox" name="myCheckbox[]" value="E" />val5
<input type="submit" name="Submit" value="Submit" />
</form>
Using [ ] in the checkbox name indicates that the selected values will be accessed by PHP script as an array. In this case $_POST['myCheckbox'] won't return a single string but will return an array consisting of all the values of the checkboxes that were checked.
For instance, if I checked all the boxes, $_POST['myCheckbox'] would be an array consisting of: {A, B, C, D, E}. Here's an example of how to retrieve the array of values and display them:
$myboxes = $_POST['myCheckbox'];
if(empty($myboxes))
{
echo("You didn't select any boxes.");
}
else
{
$i = count($myboxes);
echo("You selected $i box(es): <br>");
for($j = 0; $j < $i; $j++)
{
echo $myboxes[$j] . "<br>";
}
}
you should be able to access them from $_POST variable:
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val<br />\n";
}
It is deprecated and not wished to access superglobals directly (since php 5.5 i think?)
Every modern IDE will tell you:
Do not Access Superglobals directly. Use some filter functions (e.g. filter_input)
For our solution, to get all request parameter, we have to use the method filter_input_array
To get all params from a input method use this:
$myGetArgs = filter_input_array(INPUT_GET);
$myPostArgs = filter_input_array(INPUT_POST);
$myServerArgs = filter_input_array(INPUT_SERVER);
$myCookieArgs = filter_input_array(INPUT_COOKIE);
...
Now you can use it in var_dump or your foreach-Loops
What not works is to access the $_REQUEST Superglobal with this method. It Allways returns NULL and that is correct.
If you need to get all Input params, comming over different methods, just merge them like in the following method:
function askForPostAndGetParams(){
return array_merge (
filter_input_array(INPUT_POST),
filter_input_array(INPUT_GET)
);
}
Edit: extended Version of this method (works also when one of the request methods are not set):
function askForRequestedArguments(){
$getArray = ($tmp = filter_input_array(INPUT_GET)) ? $tmp : Array();
$postArray = ($tmp = filter_input_array(INPUT_POST)) ? $tmp : Array();
$allRequests = array_merge($getArray, $postArray);
return $allRequests;
}
So, something like the $_POST array?
You can use http_build_query($_POST) to get them in a var=xxx&var2=yyy string again. Or just print_r($_POST) to see what's there.
Why not this, it's easy:
extract($_POST);
Using this u can get all post variable
print_r($_POST)

Categories