Getting a Post Variable

Get all variables sent with POST?

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>";
}
}

Getting value GET OR POST variable using JavaScript?

You can't get the value of POST variables using Javascript, although you can insert it in the document when you process the request on the server.

<script type="text/javascript">
window.some_variable = '<?=$_POST['some_value']?>'; // That's for a string
</script>

GET variables are available through the window.location.href, and some frameworks even have methods ready to parse them.

Getting a POST variable

Use this for GET values:

Request.QueryString["key"]

And this for POST values

Request.Form["key"]

Also, this will work if you don't care whether it comes from GET or POST, or the HttpContext.Items collection:

Request["key"]

Another thing to note (if you need it) is you can check the type of request by using:

Request.RequestType

Which will be the verb used to access the page (usually GET or POST). Request.IsPostBack will usually work to check this, but only if the POST request includes the hidden fields added to the page by the ASP.NET framework.

how to get a post or get variable

1) Use $_GET if you know that data is coming via URL parameter

if (isset($_GET['htmlVariable'] && $_GET['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}

2) Use $_POST if you know that data is coming via HTTP POST method

if (isset($_POST['htmlVariable'] && $_POST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}

3) If you don't know use $_REQUEST

if (isset($_REQUEST['htmlVariable'] && $_REQUEST['htmlVariable'] != '') {
$htmlVariable = $_GET['htmlVariable'];
}

Can't get the $_POST Variable

One of your issues is that existAlbum has no actual values associated with it.

You have <option>Select Album</option> which has no value associated with the option element. If there is no value associated, the select element is not posted to the server. You should change it to be:

<option value="">Select Album</option>

EDIT

Since the user only has to supply one or the other, you should use the following to set your variables:

$existsAlbum = (isset($_POST['existAlbum']) && !empty($_POST['existAlbum'])) ? $_POST['existAlbum'] : 'defaultValue';
$newAlbum = (isset($_POST['newAlbum']) && !empty($_POST['newAlbum'])) ? $_POST['newAlbum'] : 'defaultValue';

One important thing to note is that Internet Explorer does not support the placeholder attribute.

EDIT 2

Here is my quick test page that worked test.php:

  <form action="upload.php" method="post" id="uploadform" name="uploadform" enctype="multipart/form-data">  
<label id="filelabel" for="fileselect">Choose the Pictures</label>
<input type="file" id="fileselect" class="fileuplaod" name="uploads[]" multiple />
<span class="text">Exist Album</span><br />
<select id="existAlbum" name="existAlbum" size="1">
<option value="noAlbum">SELECT ALBUM</option>
</select>
<span class="text">OR</span>
<span class="text">New Album</span><br />
<input id="newAlbum" name="newAlbum" type="text" maxlength="20" placeholder="ALBUM NAME"/>
<input type="submit" value="Submit">
</form>

upload.php

    <pre>
<?php print_r($_POST); ?>
<?php print_r($_FILES); ?>
</pre>

results

Array
(
[existAlbum] => noAlbum
[newAlbum] =>
)
Array
(
[uploads] => Array
(
//Contents here
)
)

How to use $_GET or $_POST variable to distinguish each type of request?

$_GET and $_POST superglobals exist whether passed or not, so checking for them will return true. Check entries within them to ascertain if they exist.

if(isset($_POST['submissioncount']) or
if(isset($_GET[some_GET_variable])


Alternately,

$meth = $_SERVER['REQUEST_METHOD'];
if($meth == 'GET')
//do something
else if($meth == 'POST')
//do something else

get name of a post variable

$_POST is populated as an associative array, so you can just do this:

foreach ($_POST as $name => $val)
{
echo htmlspecialchars($name . ': ' . $val) . "\n";
}

Additionally, if you're just interested in the field names, you can use array_keys($_POST); to return an array of all the keys used in $_POST. You can use those keys to reference values in $_POST.

foreach (array_keys($_POST) as $field)
{
echo $_POST[$field];
}
  • foreach documentation
  • array_keys documentation

$_POST variable name is variable, how to retrieve?

Add some quotes:

$user_input = $_POST["field$id"];


Related Topics



Leave a reply



Submit