Category: PHP

How to update session variable from dropdown change?

If you want to update your value like when drop down change then this value will set in php session in this case you can use ajax which help you lot. This is simple code which update session value when change drop down change .

This is total html and php code which need to update and browse data

<?php
session_start(); // for show your session value 
print_r($_SESSION); // remove this after check
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form name="myForm" action="/thanks" method="POST">

    <table>
        <tr>
            <td style="width:272px;">
                <select name="select_amount" id="select_amount" onchange="update_session_value(this.value)">
                    <option value="0">select a listing type</option>
                    <option value="10">Premium Listings</option>
                    <option value="20">Premium Blogs</option>
                    <option value="30">1 week sticky</option>
                </select></td>

            <td style="text-align:left;color:gray;">
                <span style="color:crimson;">*
            </span>Select a listing type
            </td>
        </tr>
    </table>
    <input type="submit" name="submit" class="button_add" onsubmit="return validateForm()">

</form>

<script>
    function update_session_value(value) {
        $.ajax({
            type: "POST",
            url: 'http://localhost/session.php', // change url as your 
            data: 'select_amount=' + value,
            dataType: 'json',
            success: function (data) {

            }
        });
    }

</script>

Then create php file which update session value

<?php
session_start();
if( isset($_POST['select_amount']) ) {
    // save values from other page to session
    $_SESSION['amount'] = $_POST['select_amount'];

}
?>

For test you can refresh the page and check output or ajax function update this code

<script>
    function update_session_value(value) {
        $.ajax({
            type: "POST",
            url: 'http://localhost/session.php', // change url as your 
            data: 'select_amount=' + value,
            dataType: 'json',
            success: function (data) {
 window.location.reload;
            }
        });
    }

</script>

if you face any problem then please inform me, I will try to help you

Comment

How to read child elements of XML using php?

If you want to get child elements from xml using php then you can use this code which help you to get child elements and some other options. This is simple demo code which help you. In this code first get child elements data then also child of child data. Just parse data using php simplexml_load_string function.

<?php
$string = <<<XML
<ORDERSTATUS>
    <DATE>23/01/18</DATE>
    <INVOICE>Test Data</INVOICE>
    <INVTOTAL Tax="25.00" Freight="28" SubTotal="80.00">100.0</INVTOTAL>
</ORDERSTATUS>
XML;

$xml = simplexml_load_string($string);

echo "DATE:".$xml->DATE."<br>";
echo "INVOICE:".$xml->INVOICE ."<br>";
echo "INVTOTAL:".$xml->INVTOTAL ."<br>";
echo  "Tax:".$xml->INVTOTAL->attributes()->Tax."<br>";
echo  "Freight:".$xml->INVTOTAL->attributes()->Freight."<br>";
echo  "SubTotal:".$xml->INVTOTAL->attributes()->SubTotal."<br>";

this code give you first child elements then also child of child elements . This is output this code

DATE:23/01/18
INVOICE:Test Data
INVTOTAL:100.0
Tax:25.00
Freight:28
SubTotal:80.00

If you face any problem then please inform me. I will try to help you.

Comment

How to rotate image and save new image using PHP ?

If you want to rotate image then save rotate image then you can use this simple php code.

<?php
$image = 'test.jpg';
 
// The file you are rotating
 
//How many degrees you wish to rotate
$degrees = 180;
 
// This sets the image type to .jpg but can be changed to png or gif
header('Content-type: image/jpeg') ;
 
// Create the canvas
$src = $image;
$system = explode(".", $src);
 
if (preg_match("/jpg|jpeg/", $system[1]))
{
$src_img=imagecreatefromjpeg($src);
}
if (preg_match("/png/", $system[1]))
{
$src_img = imagecreatefrompng($src);
}
if (preg_match("/gif/", $system[1]))
{
$src_img = imagecreatefromgif($src);
}
 
// Rotates the image
$rotate = imagerotate($src_img, $degrees, 0) ;
 
// Outputs a jpg image, you could change this to gif or png if needed
if (preg_match("/png/", $system[1]))
{
imagepng($rotate,$image);
}
else if (preg_match("/gif/", $system[1]))
{
imagegif($rotate, $image);
}
else
{
imagejpeg($rotate, $image);
}
 
imagedestroy($rotate);
imagedestroy($src_img);
 
?>

If you face any problem then please inform me. I will try to help you.

Comment

get ip from domain name using php

If you need to know ip address from domain name then you can use php which is very simple. You can use gethostbyname. gethostbyname — Get the IPv4 address corresponding to a given Internet host name . This is simple code


<?php
$ip = gethostbyname('https://techparkbd.com/');

echo $ip;
?>

Then Output is :

184.107.37.81 

If you find any problem then please inform me. I will try to help you.

Comment

How to checked checkbox is checked or not using PHP?

If you want to check that which checkbox checked and which not checked using php . Then you can check this simple code which give you this option.

<?php
if(isset($_POST['submit']))
{
  if(isset($_POST['submit']))
    {
       // $x = cout($_POST['checkbox']);
        for($x = 0;$x <= 4;$x++)
        {
            if(strlen($_POST['checkbox'][$x])>1)
            {
                echo "Checkbox is cheked<br>";
            }
            else{
                echo "Checkbox not checked<br>";
            }
        }
    }
 
 
}
 
 
?>

This is html code

<html>
<head>
 
</head>
<body>
<form enctype="multipart/form-data" action="" method="POST">
    <div id="text">
        <input type="checkbox" name="checkbox[0]"  />Checkbox1<br>
        <input type="checkbox" name="checkbox[1]"  />Checkbox2<br>
        <input type="checkbox" name="checkbox[2]"  />Checkbox3<br>
        <input type="checkbox" name="checkbox[3]"  />Checkbox4<br>
        <input type="checkbox" name="checkbox[4]"  />Checkbox5<br>
 
    </div>
    <input type="submit" value="Submit" name="submit" id="submit" />
</form>
</body>
</html>

Hope it will help you. If you find any problem then please inform me. i will try to help you

Comment

Sending data using Ajax to PHP

When you need to send data to server side then you can use Ajax to send data. This is simple code which help you.


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>PHP, jQuery search demo</title>
    <link rel="stylesheet" type="text/css" href="my.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("input").keyup(function () {
                $('#results').html('');
                var searchString = $("#search_box").val();
                var data = 'search_text=' + searchString;
                if (searchString) {
                    $.ajax({
                        type: "POST",
                        url: 'http://localhost/search.php',
                        data: data,
                        dataType: 'text',
                        async: false,
                        cache: false,
                        success: function (result) {
                            $('#results').html(result);
                            //window.location.reload();

                        }
                    });
                }
            });
        });
    </script>

</head>
<body>
<div id="container">
    <div style="margin:20px auto; text-align: center;">
        <form method="post" action="do_search.php">
            <input type="text" name="search" id="search_box" class='search_box'/>
            <input type="submit" value="Search" class="search_button"/><br/>
        </form>
    </div>
    <div>

        <div id="searchresults">Search results :</div>
        <ul id="results" class="update">
        </ul>

    </div>
</div>

</body>
</html>

then create a search.php file which process ajax request value. you can also select data from database using this ajax data, also set this data to session etc.

This is search.php file

<?php 

$searchquery = $_POST['search_text']; 
echo $searchquery;
?>

if you face any problem then please inform me.

Comment

How to textarea lines convert to array using php?

If you want to textarea lines convert to array using php then check this demo code which help you.

Important thing that your textarea must need new line. In this code i added
for new line for better understand

$text = "Name:Jay <br/>
Email:jayviru@demo.com <br/>
Contact:9876541230";
$array_data = explode('<br/>', $text);

$final_data = array();
foreach ($array_data as $data){
    $format_data = explode(':',$data);
    $final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);

Then output is :

Array
(
    [Name] => Jay
    [Email] => jayviru@demo.com
    [Contact] => 9876541230
)

And if your text area value get using php variable then simple use this code

<?php
$text = $_POST['filed_value'];
$array_data = explode(PHP_EOL, $text);

$final_data = array();
foreach ($array_data as $data){
    $format_data = explode(':',$data);
    $final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);

This is get same value, if you face any problem then please inform me. I will try to help you

Comment

how to convert multidimensional array to single array using php?

For convert multidimensional array to single array then this is php code which help you lot . just follow this code

<?php
$your_array = array(0 => array('payment_id' => 3160), 1 => array('action' => 'update'), 2 => array('date' => '2017-05-17 09:59:40'), 3 => array('payment_date' => '23.05.201'));
echo "<pre>";
print_r($your_array);
$convert_array = array_reduce($your_array, 'array_merge', array());
echo "<pre>";
print_r($convert_array);

if you run this code then you will see this output like this :

Input array :

Array
(
    [0] => Array
        (
            [payment_id] => 3160
        )
 
    [1] => Array
        (
            [action] => update
        )
 
    [2] => Array
        (
            [date] => 2017-05-17 09:59:40
        )
 
    [3] => Array
        (
            [payment_date] => 23.05.201
        )
 
)

then output is :

Array
(
    [payment_id] => 3160
    [action] => update
    [date] => 2017-05-17 09:59:40
    [payment_date] => 23.05.201
)

if you face any problem then inform me, i will try to help you.

Comment

Multidimensional array sorting with key value using php

If you want to sorting multidimensional array using php then check this code , hope it will help you.

 <?php
$yourarray = array(
    0 => array(
        'bill_number' => 3,
        'amount' => 100
    ),
    1 => array(
        'bill_number' => 4,
        'amount' => 50
    ),
    2 => array(
        'bill_number' => 5,
        'amount' => 150
    ),
);
 
function sortByOrder($a, $b)
{
    return $a['amount'] - $b['amount'];
}
 
usort($yourarray, 'sortByOrder');
echo "<pre>";
print_r($yourarray);
?>

this code will give this output :

Array
(
    [0] => Array
        (
            [bill_number] => 4
            [amount] => 50
        )
 
    [1] => Array
        (
            [bill_number] => 3
            [amount] => 100
        )
 
    [2] => Array
        (
            [bill_number] => 5
            [amount] => 150
        )
 
)

View 1 Comment

Phpstrom Editor Some Important Tips


-Xms128m
-Xmx512m
-XX:MaxPermSize=250m
-XX:ReservedCodeCacheSize=64m
-XX:+UseCodeCacheFlushing
-ea
Just change Xmx 512 m this value. then save it. sometime this file not save normally then save it other folder then replace this in bin folder. Restart phpstrom editor .You will see new define memory in right bottom side.

2. How to show line number ?

Ans: goto file->setting from top menu then you will see a screen . goto <span style="color: #993300;">editor tab-> appearance</span> then you will see

Show line number checkbox . Check it.

3. Goto line number ->Ctrl+g

4. Find Ctrl+f

5. Find and replace Ctrl+r

6. Find in path Ctrl+shift+f

7. Replace in path Ctrl+shift+r

8. Any comment ctrl+shift+/

9. Goto end tag ctrl+}

10. Format code ctrl+alt+l

11. goto include file then click this file name

12. Goto start tag ctrl+{
13. Find total function list and file list which include ctrl+F12

For more setting go to file->setting from top menu then go to keymap from "editor->keymap".



 

if you face any problem then please inform me.

Comment