将变量作为ajax响应发送

时间:2013-12-31 03:00:38

标签: php jquery ajax

我正在通过ajax发布表单并在表单页面中打印响应。

的index.php:

<script src="jquery.min.js"></script>
<script src="jquery.form.min.js"></script> 
<script type="text/javascript">
    function a(){alert('ok');
$(function(){ 
    $('#submitForm').ajaxForm(function(result){
                    alert('the form was successfully posted!');
                    document.getElementById('resulte').innerHTML=result;
                }).submit();
         });
         }
</script>
    <body>
    <form id="submitForm" name="submitForm" action="upload.php" method="post" enctype="multipart/form-data" >
    File: <input name="myfile" type="file" />
    <input type="button" id="submitBtn" name="submitBtn" value="Upload" onclick="a();" />
    </form>
    <div id="resulte" name="resulte">a</div>
    <span id="usererr" name="usererr" class="formerror"></span>
    <span id="passerr" name="passerr" class="formerror"></span>
    <span id="passconferr" name="passconferr" class="formerror"></span>
    </body>

upload.php的:

<?php 
$result = 0; 
$target_path = 'uploads/' . basename( $_FILES['myfile']['name']); 
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
} 
sleep(1);
echo"good";
$arr=array('username'=>array('isEmpty'=>'required','exists'=>'username exists'),'password'=>array('isEmpty'=>'required'))
?>

我想通过ajax将$ arr发送到'index.php'并显示'usererr'和'passerr'spans中的值

回显'index.php'中不同跨度的每一行。我怎么能这样做?

-Thanks。

修改

我做了以下但是现在没有文字在'usererr'范围内打印,警报框没有显示。我做错了吗? (表单已过帐,文件已成功保存,但问题与'响应'有关)

的index.php:

<script src="jquery.min.js"></script>
<script src="jquery.form.min.js"></script> 
<script type="text/javascript">
function a(){ 
    $('#submitForm').ajaxForm({
        dataType: 'json',
        success: function(result){        
                    alert('the form was successfully posted!');
              $('#usererr').html(result['username']['isEmpty']);
                }}).submit();
         }</script>

    <body>
    <form id="submitForm" name="submitForm" action="upload.php" method="post" enctype="multipart/form-data" >
    File: <input name="myfile" type="file" />
    <input type="button" id="submitBtn" name="submitBtn" value="Upload" onclick="a();" />
    </form>
    <div id="resulte" name="resulte">a</div>
    <span id="usererr" name="usererr" class="formerror"></span>
    <span id="passerr" name="passerr" class="formerror"></span>
    <span id="passconferr" name="passconferr" class="formerror"></span>
    </body>

upload.php的:

<?php 
$result = 0; 
$target_path = 'uploads/' . basename( $_FILES['myfile']['name']); 
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
}
$arr=array('username'=>array('isEmpty'=>'required','exists'=>'username exists'),'password'=>array('isEmpty'=>'required'));
echo json_encode($arr);
?>

3 个答案:

答案 0 :(得分:1)

您必须将数组解析为JSON:

json_encode();

然后你可以回应结果。

在JavaScript中,您可以解码它。因为您已经在使用jQuery,所以可以从'result'获取数组中的数据。像result.username.isEmpty

一样访问它

答案 1 :(得分:1)

您可以将json用作dataType,例如:

$('#submitForm').ajaxForm({
   dataType: 'json',
   success: function(result){
       //get the required values from result like result.username.isEmpty
       console.log(result);
   }).submit();
});

json_encode()要在PHP中返回的数组,如:

...
$arr=array('username'=>array('isEmpty'=>'required','exists'=>'username exists'),'password'=>array('isEmpty'=>'required'))
echo json_encode($arr);

请参阅:ajaxForm

答案 2 :(得分:1)

在upload.php中尝试echo json_encode($arr);。 在你的ajaxForm插件中,将选项设置为json,如下所示

 $('#submitForm').ajaxForm({
                dataType:  'json',
                success:   function(result){
                      $('#usererr').html(result['username']['isEmpty']);   
                      $('#passerr').html(result['password']['isEmpty']);
                },                  
            }).submit();