Blessan Kurien Blessan Kurien - 3 months ago 6
PHP Question

Reading json string from textfield in php when form submitted

I am trying to read json string from input hidden filed on form submit

<form action="<?=base_url()?>update-basic" method="post" enctype="multipart/form-data">
<input type="hidden" name="old_image" value="<?=json_encode($package_detail->slider_image)?>">
<button type="submit" value="Update" class="btn btn-sm btn-primary">Update</button>
</form>


Here the json string comes from database i want to get the json string in server side when the form is submitted

PHP

$json=file_get_contents('php://input');
var_dump($json);


but i will get empty string as the value of
$json


However the json string correctly printed in html

<input type="hidden" name="old_image" value=""{\"0\":\"FB_IMG_1.jpg\"}"">


I am using codeigniter

Answer

try this

<input type="text" name="old_image" value='{"0":"FB_IMG_1.jpg"}' />

<?php

  $ss = '{"0":"FB_IMG_1.jpg"}';

 echo "<PRE>";
 print_r(json_decode($ss,true));
 echo "</PRE>";

?>

OUTPUT :

Array
(
    [0] => FB_IMG_1.jpg
)