Wednesday, 23 March 2016

How to send ajax request in PHP?

How to send ajax request in PHP?

HTML Code



<!--- HTML Part --->
<form id="formId" method="POST" onsubmit="return saveData()">
<input name="formsubmit" type="hidden" value="1" />
    First Name:  <input name="first_name" type="text" />
    <br />
Last Name:  <input name="last_name" type="text" />
    <br />
Email: &nbsp;   &nbsp;  &nbsp;   &nbsp;<input name="email" type="text" /><br />
<input type="submit" value="Click to Submit" />
</form>
<!--- HTML Part --->



jQuery Code



<!--- Javascript Part --->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
    function saveData() {
        
        $.ajax({
            url: "ajaxfile.php",
            method:"post",
            data: $('form#formId').serialize()+'&phone=789797522&city_id=1',
            cache: false,
            success: function(msg) {
                console.log(msg);
            }
        });
        return false;
    }

</script>
<!--- Javascript Part --->



PHP Code



if(!empty($_POST)){
    echo "<pre>";
    print_r($_POST);
 echo "</pre>";
    /** You can write your code here **/    
    

/** You can write your code here **/        
    die;
}



PHP Output Code



Array
(
    [formsubmit] => 1
    [first_name] => Raj
    [last_name] => sinha
    [email] => raj@no-spam.ws
    [phone] => 789797522
    [city_id] => 1
)





Tuesday, 22 March 2016

How to decode json string in PHP?

How to decode json string in PHP?

Question: Give an Sample of Correct JSON Object?
[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]



Question: Can we check, JSON is valid OR Not?
Yes, we can do.
Below function check that JSON is valid or Not. (1 for valid, 0 for invalid)
function isValidJson($string) {
 json_decode($string);
 return (json_last_error() == JSON_ERROR_NONE);
}



Question: How to check JSON is valid?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
if(isValidJson($string)){
    echo "JSON is Valid"; //This will print
}else{
    echo "JSON is NOT Valid";
}



Question: How to check JSON is NOT valid?
$string='[{{{{{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
if(isValidJson($string)){
    echo "JSON is Valid"; 
}else{
    echo "JSON is NOT Valid";//This will print
}



Question: How to check JSON is empty OR Not?
$string='{}';
$data = json_decode($string);
if(empty($data) || count($data)){
    echo "JSON is empty";
}else{
    echo "JSON is NOT empty";
}



Question: How to create an empty JSON?
$string='{}';



Question: How to decode JSON string in PHP?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
$data = json_decode($string);



Question: How to create JSON from php array?
$array = array('name'=>'PHP Tutorial','Description'=>'Web technology experts notes');
echo json_encode($array);



Question: What is the Correct content-type of JSON?
application/json


Question: How to convert JSON Object into PHP Object?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
$data = json_decode($string,false);
print_r($data);



Question: How to convert JSON Object into PHP Array?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
$data = json_decode($string,true);
print_r($data);



Question: How to get JSON error?
$string='Invalid JSON';
json_decode($string);
echo json_last_error(); //4



Question: How to get JSON error Message?
$string='Invalid JSON';
json_decode($string);
echo json_last_error_msg (); //Syntax error