Some time post data return null , Jquery , PHP

Go To StackoverFlow.com

0

I am trying to post data from PHP to Server which is built in java using jquery.It responding with json. Following PHP , Jquery post code

    <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>

This modal form which send the data to server

<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>

This is PHP code which takes the Posted data and send to server and return 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}

Sometime above code runs WELL But sometime returns null.Unable to track this problem

2012-04-04 04:41
by Manzur Husain
"which is built in java using jquery" gotta love e - NoName 2016-04-20 06:28


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);
    }
2012-04-04 05:03
by coolguy
THanks for the reply @ubercooluk.. Still returning null value not error(i.e Your post has some problem - Manzur Husain 2012-04-04 07:56
Ads