user2501504
user2501504

Reputation: 311

Jquery with Json and PHP

Hi i have post form in jquery with php , the form call other page and get the result with json from jquery , for example when i use jquery and call to this page in php i get this for read json from jquery :

<?php
print '{"login":"datos_fails_lock"}';
?>

The problem in this case it´s i can only show this and no include other text in the results , if include more os this code the script no works , i like json detect this code but also i want show html and at the moment i can´t do this

It´s posible when i call other page from form of jquery i can include other informations as html and works json from this page

Sorry i explain the best i can

Regards !

Upvotes: 1

Views: 95

Answers (4)

user1855153
user1855153

Reputation: 579

The best inline solution:

<?php
    $myObjectOrArray = (object) array('test' => array('test' => array('test' => 'testValue')));
    $javascriptGateway = rawurlencode(json_encode(myObjectOrArray));
?>

<script type="text/javascript">
    //<!--
         var myObjectOrArray = JSON.stringify(unescape('<?php echo $javascriptGateway;?>'));
         alert(myObjectOrArray.test.test.test); //result: testValue
    //-->
</script>

I use this solution so I can pass my php object containing special char into a javascript object.

Upvotes: 0

Sujit Poudel
Sujit Poudel

Reputation: 541

To the best I can understand, if you are looking to get json encoded values from your php file and show with jquery that called the file you do something like:

In you php file:

<?
  echo json_encode(array(
    'login' => 'datos_fails_lock',
    'something_else' => 'Other thing'
  ));
?>

and from jQuery if it is post submission, you can use some thing like:

$.post('ajax/process.php', function(data) {
  $('.result').html(data.login);
  $('.other_thing').html(data.something_else);
});

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

At the PHP side:

<?php
  // compute html
  $html = '<div><p>...</p></div>';
  // compute json
  $json = '{"login":"datos_fails_lock"}';

  $response = array('html' => $html, 'json' => $json);
  print json_encode($response);
?>

At the JavaScript side:

var response = JSON.parse( xhr.responseText );

// append html to a div
$( '#response' ).html( response.html );
// parse json response
var jsonObj = JSON.parse( response.json );

Upvotes: 0

Mic1780
Mic1780

Reputation: 1794

Instead of printing your own json text layout, why not use PHP json_encode to do it for you. that way you can have the array you want including html and have that function encode it.

Example:

<?php
print json_encode(array('login' => 'datos_fails_lock'));
?>

Upvotes: 1

Related Questions