PHP FAQ - 1

Monday 14 September 2015


Q-1) I would like to write a generic PHP script that can handle data coming from any form. How do I know which POST method variables are available?
  • PHP provides a pre-defined super global variable called $_POST, by using this we can get the posted value.
  • Basically $_POST is a associative array contains form input element names and values as Key and Value respectively.
  • If we apply loop to $_POST, we will get each key and value, and by using empty() we can find out the empty values.
<?php
        if(isset($_POST['sub'])){
                foreach($_POST  as  $key => $val){
                        if(empty($val)){
                                $empty[$key] = $val;
                        }else{
                                $post[$key] = $val;
                        }
                }
               
                echo "<pre>";
                print_r($empty);
                echo "</pre>";
                echo "<pre>";
                print_r($post);
                echo "</pre>";
        }else{
                echo "Not POSTed !";
        }
?>

Q-2) How am I supposed to mix XML and PHP? It complains about my <?xml tags! ?

  • In order to embed <?xml straight into your PHP code, you'll have to turn off short tags by having the PHP directive short_open_tags set to 0.
  • You cannot set this directive with ini_set(). Regardless of short_open_tags being on or off, you can do something like: <?php echo '<?xml'; ?>. The default for this directive is On.

Q-3) How can I generate PDF files without using the non-free and commercial libraries like PDFLib? I'd like something that's free and doesn't require external PDF libraries.

  • There are a few alternatives written in PHP such as » FPDF and » TCPDF.
  • There is also the Haru extension that uses the free libHaru external library.

Q-4) How do I create arrays in a HTML <form>?
To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:
Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It's also possible to assign specific keys to your arrays:
<input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />
The AnotherArray array will now contain the keys 0, 1, email and phone.

Q-5) How do I get all the results from a select multiple HTML tag?
The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. i.e.
<select name="var" multiple="yes">
Each selected option will arrive at the action handler as:
var=option1
var=option2
var=option3
Each option will overwrite the contents of the previous $var variable. The solution is to use PHP's "array from form element" feature. The following should be used:
<select name="var[]" multiple="yes">
This tells PHP to treat $var as an array and each assignment of a value to var[] adds an item to the array. The first item becomes $var[0], the next $var[1], etc. The count() function can be used to determine how many options were selected, and the sort() function can be used to sort the option array if necessary.
Note that if you are using JavaScript the [] on the element name might cause you problems when you try to refer to the element by name. Use it's numerical form element ID instead, or enclose the variable name in single quotes and use that as the index to the elements array, for example:
variable = document.forms[0].elements['var[]'];
How can I pass a variable from Javascript to PHP?
Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables.

It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this -- it allows PHP code to capture screen height and width, something that is normally only possible on the client side.
<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
  // output the geometry variables
echo "Screen width is: ". $_GET['width'] ."<br />\n";
echo "Screen height is: ". $_GET['height'] ."<br />\n";
} else {
  // pass the geometry variables
  // (preserve the original query string
  //   -- post variables will need to handled differently)

echo "<script language='javascript'>\n";
echo "  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
            . "&width=\" + screen.width + \"&height=\" + screen.height;\n";
echo "</script>\n";
exit();
}

?>

No comments:

Post a Comment

 
About Contact Career Advertise Job Support Online Training Acardemic Projects Internship
Copyright © 2016. [ info ].
Design by Herdiansyah Hamzah. & Distributed by Free Blogger Templates
Creative Commons License