PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: foreach
foreach - phponwebsites.com
Showing posts with label foreach. Show all posts

13 Dec 2015

Get multiple selected values from drop down box using PHP

                           You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Now the doubt is rise. How to select multiple values from drop down box and how to get multiple selected input data from drop down box in PHP. This post describes retrieve multiple selected values from drop down box using PHP.

How to select multiple values from drop down box in HTML


                           You know select single value in drop down box. Then how to select multiple values in drop down box. You have to add attribute "multiple" and set name as array in drop down box in order to select multiple values from drop down box. If you want to select more than one values in drop down box, then press "ctrl" while selecting values in drop down box. Let consider below example to make HTML formwith select multiple values in drop down box.

<!DOCTYPE html>
<html>
<body>
<form action="#" method="post">
<select name="country[]" multiple>
  <option value="USA">USA</option>
  <option value="United Kingdom">United Kingdom</option>
  <option value="Russia">Russia</option>
  <option value="Brazil">Brazil</option>
  <option value="India">India</option>
</select>
<input type="submit" name="submit">
</form>
</body>
</html>

Retrieve multiple selected values from drop down box using PHP


                                 You can get value from selection box using $_POST['selection_box_name'] ( if form method is post) in PHP. Then display each selected items through foreach loop in PHP. The PHP script describes how to select multiple selected values from drop down box.

<!DOCTYPE html>
<html>
<body>
<form action="#" method="post">
<select name="country[]" multiple>
  <option value="USA">USA</option>
  <option value="United Kingdom">United Kingdom</option>
  <option value="Russia">Russia</option>
  <option value="Brazil">Brazil</option>
  <option value="India">India</option>
</select>
<input type="submit" name="submit">
</form>
<?php
 if(isset($_POST['submit']))
 {
  $country=$_POST['country'];
  foreach($country as $val)
  {
    echo $val.'<br>';
  }
 }
?>
</body>
</html>

               
                                      While submitting form with selected multiple values from drop down box, you can get multiple selected values using PHP.

1 Jun 2015

Upload multiple files/images using PHP

                        You can upload single file and image easily in PHP. Now we are going to know how to upload multiple files and images using PHP. You can upload more than one images or files at the time using PHP. It can done using by array concepts.

Form for upload multiple images:

               
                     First you need to make form for upload more images. Your form should be like following:

<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" id="file" multiple="multiple">
<input type="file" name="file[]" id="file" multiple="multiple">
 <input type="submit" id="submit" name="submit" value="submit">
 </form>

                  You should add enctype="multipart/form-data" for upload files. You can see above where the name of file tag should be array like name="file[]" for store multiple values.

Upload multiple images using PHP:


                       Similarly upload single image, you will get image values by $_FILES['file']['name'] in PHP while submitting a form. You need temporary name of image to pass one of the parameter in move_uploaded_file() in order to upload files. If you've a single image, then you can upload easily by move_uploaded_file($_FILES['file']['tmp_name'],$name). But you need to upload multiple images. So you've to get values as an array. Then upload each image using loop. Consider the following example. Here it describes how to get temporary name of each image one by one using loop in PHP.


<?php
if(isset($_POST['submit']))
{
 $name=$_FILES['file']['name'];
 foreach($name as $val => $name1)
 {
   $file_name=$name1;
   $tmp=$_FILES['file']['tmp_name'][$val];
  move_uploaded_file($tmp,$_SERVER['DOCUMENT_ROOT'].'img/'.$file_name);
 }
}

?>
<html>
<body>
<form action="#" method="post" enctype="multipart/form-data">
<table>
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr>
<tr><td><input type="file" name="file[]" id="file" multiple="multiple"></td></tr>
<tr><td><input type="submit" id="submit" name="submit" value="submit"></td></tr>
</table>
</form>
</body>
</html>

             
     
     Where,
                move_uploaded_file() - to upload files to desired location.
                $_SERVER['DOCUMENT_ROOT'] gives the root directory of current script executing.
You can upload multiple files as well as images using this PHP script.

Related Posts:


13 May 2015

Get multiple values from checked checkboxes using PHP

                      Normally you can get single value from textbox, checkbox and radio buttons. Can you retrieve multiple values from selected checkbox.? Yes you can get multiple values from checked checkbox using PHP.

Get value from checked checkbox using PHP:


                         You can retrieve value from single checkbox using php and html. The following php code is used for get values from checked checkbox.

<html>
<body>
<?php
if(isset($_POST['submit']))
{
echo'You checked following value:<br>';
echo $_POST['check'];
}
else
{
?>
<form action="#" method="post">
<input type="checkbox" name="check" value="apple">apple <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
}
?>
</body>
</html>


Retrieve values from multiple checked checkboxes using php:


                       If you want to get multiple values from checked checkbox, then you use name as an array for your checkbox. It should be like following.

<input type="checkbox" name="check[]" value="apple">apple
<input type="checkbox" name="check[]" value="orange">orange
<input type="checkbox" name="check[]" value="mango">mango

The following php code is used to get values from multiple checked checkbox.


<html>
<body>
<?php
if(isset($_POST['submit']))
{
echo'You checked following value(s):<br>';
 foreach($_POST['check'] as $val)
 {
 echo $val.'<br>';
 }
}
else
{
?>
<form action="#" method="post">
<input type="checkbox" name="check[]" value="apple">apple <br>
<input type="checkbox" name="check[]" value="orange">orange <br>
<input type="checkbox" name="check[]" value="mango">mango <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
}
?>
</body>
</html>


     where ,
                  $_POST['check'] is used to get values from checkbox using php.
                  foreach() is used to get array values from checkbox and display them

When you run this php file, your output like as below:

get values from multiple check box

Suppose, you select check boxes like below:

get values from checked checkbox using php

You'll get output like as below

                        You checked following value(s):
                         apple
                         mango


23 Jun 2014

How to store array values into session in php

                You can store single value to session in PHP easily. Visit store values to session in php.


Store array values to session in PHP:


                          You can also store multiple values into session using PHP. You can store one or more than one values in array that can be stored into session. Consider the following example. It describes the array values directly stored into session in PHP. The following PHP script is used to store values to session in PHP.

<?php
 session_start();
 $ar=array('php','mysql','jquery');
 $_SESSION['arr']=$ar;
 $session=$_SESSION['arr'];
 foreach($session as $val)
 {
   echo $val.'<br>';
 }
 ?>

 where,
            session_start() - start the session
            $ar=array() - array variable

                  Now you can get output like this:

                                    php
                                    mysql
                                    jquery

Store multiple (array) values into session in PHP on form submit:

                          Consider the following example:


 <html>
 <body>
 <?php
 session_start();
 error_reporting('E_ALL ^ E_NOTICE');
 if($_POST['submit'])
 {
  if(count($_SESSION['arr'])==0)
  {
   $ar=array();
   $val=$_POST['value'];
   array_push($ar,$val);
   $_SESSION['arr']=$ar;
   print_R($_SESSION['arr']);
  }
  else
  {
   $val=$_POST['value'];
   array_push($_SESSION['arr'],$val);
   print_R($_SESSION['arr']);
  }
 }
?>
 <form action="#" method="post">
  <input type="text" name="value">
  <input type="submit" name="submit" value="Submit">
 </form>
 </body>
 </html>


                    The above example describes array values are storing into session in PHP while form submitting.
            where,
                      if(count($_SESSION['arr'])==0) {} - check  whether the array contains values or not. Create array variable and store value into array, then value is stored into session if there is no values in session. Otherwise it directly add values into session.
                     array_push() - add values to array in PHP.

Related Post: