2012-04-04 72 views
0

我想從PHP發佈數據到服務器,它是使用jQuery建立在Java中,它與JSON響應。 下面的PHP,jQuery的崗位代碼一些時間發佈數據返回null,jquery,PHP

<script type="text/javascript" charset="utf-8"> 

    $(function() { 
     // wire up the buttons to dismiss the modal when shown 
    $("#dialog-form").bind("show", function() { 
     $("#dialog-form #add-ques").click(function(e) { 
      // do something based on which button was clicked 
      // we just log the contents of the link element for demo purposes 
      $.post('question_data.php', $("form#gform").serialize(), function(data) { 


          if(data.result=="true"){ 
            //window.location=+data.lastId; 
            console.log(data); 

           }else{ 

            $("#dialog-form").modal('hide'); 

           } 

         },"json"); 

      // hide the dialog box 
      $("#dialog-form").modal('hide'); 

     }); 

    }); 

</script> 

這將數據發送到服務器

<div id="dialog-form" class="modal hide fade"> 
     <div class="modal-header"> 
      <a class="close" data-dismiss="modal">×</a> 
      <h3>Add Question</h3> 
     </div> 
     <div class="modal-body"> 
      <form id=gform > 

       <div class="control-group" class="form-horizontal"> 
         <label class="control-label" for="question">Write Your Question</label> 
         <div class="controls"> 
          <input type="text" class="span3" id="question" 
           name="question" placeholder="Enter Survey Question"> 
           <input type="hidden" value=save id="type" name="type" /> 
           <input type="hidden" value=12 id="survey_id" name="survey_id" /> 

         </div> 
        </div> 

       <hr/> 
       <label for="name">Write Options</label> 
          <div class="controls"> 
       <input type="text" name="option1" id="option1" placeholder="Option 1" /> 
       <input type="text" name="option2" id="option2" placeholder="Option 2" /> 
         </div> 

          <div class="controls"> 
       <input type="text" name="option3" id="option3" placeholder="Option 3" /> 
       <input type="text" name="option4" id="option4" placeholder="Option 4" /> 
         </div> 










      </form> 

     </div> 
     <div class="modal-footer"> 
      <a href="#" class="btn" data-dismiss="modal" >Close</a> <a href="#" id="add-ques" class="btn btn-primary">Save 
       changes</a> 
     </div> 

    </div> 

模式窗體這是PHP代碼,這需要在發佈數據發送到服務器,並返回JSON // question_data .PHP

if($type=="save"){ 
    $type= htmlspecialchars(trim($_REQUEST["type"])); 
$question= htmlspecialchars(trim($_POST["question"])); 
$survey_id= htmlspecialchars(trim($_POST["survey_id"])); 
$opt1= htmlspecialchars(trim($_POST["option1"])); 
$opt2= htmlspecialchars(trim($_POST["option2"])); 
$opt3= htmlspecialchars(trim($_POST["option3"])); 
$opt4= htmlspecialchars(trim($_POST["option4"])); 


$result = json_decode(file_get_contents("http://mobsurvey.jelastic.servint.net/question/new/?question=$question&survey_id=$survey_id&option1=$opt1&option2=$opt2&option3=$opt3&option4=$opt4")); 
$arr = array('result' => $result->result, 'lastId' => $result->lastId); 

    header('Content-type: text/json'); 
    header('Content-type: application/json'); 

    echo json_encode($arr); 

} 

JSON O/P

{"result": "true","lastId": 86} 

有時上面的代碼運行WELL但有時返回 .Unable跟蹤這個問題

+0

得愛他們 – 2016-04-20 06:28:59

回答

0
//Track it on the server side 

if($type=="save"){ 
     $surveyname= htmlspecialchars(trim($_POST["name"])); 
     $company_id= htmlspecialchars(trim($_POST["company_id"])); 
     $flag = 1; 
     if($surveyname == null || $surveyname == ""){ 
      $flag = 0; 
     } 
     if($company_id == null || $company_id == ""){ 
      $flag = 0; 
     } 

    if($flag){ 
     $result = json_decode(file_get_contents("http://localhost:8080/mobsurvey/survey/new/?name=$surveyname&company_id=$company_id")); 
     $arr = array('result' => $result->result, 'lastId' => $result->lastId); 

     header('Content-type: text/json'); 
     header('Content-type: application/json'); 


     }else{ 
    $arr = array('Your post has some problems..not posting some of the values'); 
    } 
     echo json_encode($arr); 
    } 
+0

感謝您的答覆@ubercooluk「這是使用jQuery Java構建」。 。仍然返回空值不錯誤(即您的帖子有一些問題) – 2012-04-04 07:56:03