Theo
Theo

Reputation: 371

Getting JSON from PHP

I am having the following registration script,where I the first name,email etc are inserted in the database via simple html form.

<?php

include("connect.php");

$error = "";

$response[]=array();

if(isset($_POST['submit'])){

    $firstName = $_POST['fname'];
    $lastName = $_POST['lname'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passwordConfirm = $_POST['passwordConfirm'];
    $image = $_FILES['image']['name'];
    $temp_image = $_FILES['image']['tmp_name'];
    $imageSize = $_FILES['image']['size'];

    //echo $firstName."<br/>".$lastName."<br/>".$email."<br/>".$password."<br/>".$passwordConfirm."<br/>".$image."<br/>".$imageSize."<br/>";

    if(strlen($firstName)<3){

        $error = "First name is too short";

    }else if(strlen($lastName)<3){

        $error = "Last name is too short";

    }else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){

        $error = "Please enter valid email address";

    }else if(strlen($password)<8){

        $error = "Password must be greater than 8 characters";

    }else if($password !== $passwordConfirm){

        $error = "Password does not match";

    }else if($image == ""){

        $error = "Please upload your image";

    }else{

        $insertQuery = "INSERT INTO users(firstName,lastName,email,password,image) VALUES('$firstName','$lastName','$email','$password','$image')";
        $result = mysqli_query($con,$insertQuery);
        if($result){
            //upload image in images forlder
            $sql = "SELECT * FROM users";
            $selectQuery = mysqli_query($con,$sql);
            if($selectQuery){
                while($row = mysqli_fetch_assoc($selectQuery)){
                    $response[] = $row; 




                }
        }


            if(move_uploaded_file($temp_image,"images/$image")){
                $error = "You are successfully regirestered";

            }else{
                $error = "Image is not uploaded";
            }   
        }
    }

}

?>

<?php
echo $error."<br/>";
echo json_encode($response);
?>

All I am doing is validating the form,inserting data and finally selecting all data from the database so they can be represented as a JSON format.

The JSON I am getting is this.

[[],{"id":"37","firstName":"Theo","lastName":"Tziomakas","email":"[email protected]","password":"testingpass","image":"56a5e6dc-d102-4f9a-bd5b-e6a217e5ad97.png"}]

how can I take the empty array

[]

out of the JSON response ie.

[{"id":"37","firstName":"Theo","lastName":"Tziomakas","email":"[email protected]","password":"testingpass","image":"56a5e6dc-d102-4f9a-bd5b-e6a217e5ad97.png"}]

Also how can I generate a JSON like this?

{"status":"success","message":"Successfully registered"}

Upvotes: 1

Views: 80

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

To remove the erroneous [] at the beginning of the JSON structure change this line

$response[]=array();

To

$response = array();

To generate a JSON structure like this

{"status":"success","message":"Successfully registered"}

I would do this

$j = new stdClass();
$j->status  = 'success';
$j->message = 'Successfully registered';

$string = json_encode($j);

Upvotes: 2

Related Questions