Unknown developer
Unknown developer

Reputation: 5950

JSON does not work in ajaxForm

It is just impossible. I cannot find what is wrong!!Consider the following very simple testing code:

<head>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script src="js/jquery.form.js"></script>   <!--AJAX JQUERY FORM PLUGIN-->
</head> 
<body>   

<script>
$(document).ready(function() { 
    $('#form1').ajaxForm({ 
        dataType:  'json',      
        success: function () { 
           alert(5);    
        }            
    });
}); 
</script>

<form action="proceed.php" method="post" name="form1" id="form1" >
   <input type="submit" name="one_button" id="one_button" value="GO" />
</form>
</body>

and the code for proceed.php:

<?php
  $message= 'success';  
  echo json_encode($message); 
?> 

The above code does not alert '5' on button click unless I subtract line dataType: 'json'. Is the problem in PHP which does not send correctly the json data? I cannot tell... The crazy about that is I have used exactly this code many times and everything was fine!!!

Upvotes: 0

Views: 124

Answers (2)

pguetschow
pguetschow

Reputation: 5337

 $message= 'success';  

must be an array;

 $message[]= 'success';  

Upvotes: 0

Rajveer gangwar
Rajveer gangwar

Reputation: 735

Please your $message as array. Your code must be look like this.

<?php
  $message[]= 'success';  
  echo json_encode($message); 
?>

http://php.net/manual/en/function.json-encode.php

Upvotes: 1

Related Questions