2

I'm trying to retrieve four values from a post request but I only get 2 of them. The form contains an id, step, name, and email but I only get the ones from the hidden inputs. And not the ones that the user fills in. I think that it might be the jquery serialize() but i'm not sure.

I have tried to change the inputs to hidden and then add value="something" and it works. Why doesent it work with regular text?

//result from post request:

[position_id] => 229
[step] => 1
[name] => 
[email] => 

The form looks like this:

<form id="referral-form" action="#" method="post">

    <input type="hidden" name="position_id" value="{{$position->id}}" />
    <input type="hidden" name="step" value="1" />

    <div class="form-group">
        <input name="name" class="form-control" type="text" id="name" required/>
    </div>

    <div class="form-group">
        <input name="email" class="form-control" type="text" id="email" required />
    </div>
    <div id="legal">
        <span class="loader-button"></span>
        <div class="button submit"></div>
    </div>

</form>

I have a javascript method that get initiated if user clicks on .button.submit

onRef: function(e) {
    e.preventDefault();
    var $form = $("#referral-form");

    if(!$form.hasClass("ajax"))
    {
        $form.addClass("ajax");
        $form.find('.error').css('display', 'none');

        var req = $.post('/reff/ref', $form.serialize());

        req.done(function(res) {
            $form.removeClass("ajax");

            if(res.success) {
                //do somthing
            }
            else {
                methods.printErrors(res.errors, $form);

                mixpanel.track('onReferralValidationFailure', {
                    errors: res.errors,
                    positionId: exported.position.id,
                    companyId: exported.position.company_id
                });
            }
        });

        req.fail(function() {
            $form.removeClass("ajax");

            mixpanel.track('onReferralUnknownError', {
                positionId: exported.position.id,
                companyId: exported.position.company_id
            });
        });
    }
},

And when I try to retrieve the post data within the /reff/ref function in php I only get this data.

[2016-07-07 11:58:39] local.INFO: Array
(
    [position_id] => 229
    [step] => 1
    [name] => 
    [email] => 
)

This is the function in reff/ref:

 public function refer() {
    $positionId = Input::get("position_id");
    Language::setLanguageByPositionId($positionId);

    if(Input::get("step") == 1) {
        $validator = new ReferralStepOneValidator(App::make('validator'));
        $validator->with(Input::all());

        Log::info(print_r(Input::all(), true));

        if($validator->passes()) {
            $input = Input::all();


            $referral = Referral::createReferralFromInput($input);

            return Response::json(array(
                'success' => true,
                'reference' => $referral->reference,
            ));
        }
        else {
            return Response::json(array(
                'success' => false,
                'errors' => $validator->errors()->toArray(),
            ));
        }
    }
10
  • Can you show what's in /reff/ref? Commented Jul 7, 2016 at 10:18
  • Make sure $form has all the correct data and $form.serialize() doesn't do anything funny to them. Commented Jul 7, 2016 at 10:25
  • You can check your sent data (Chrome: developers tool, network) and verify which params are being sent in the post request. Commented Jul 7, 2016 at 10:25
  • @awl19 i have added the refer ref Commented Jul 7, 2016 at 10:26
  • The problem is in /reff/ref - I'm guessing you're using a framework I'm not familiar with, so I can't tell exactly how you're passing the variables through the JSON array, but if you do a search for "user" or "email" in /reff/ref you'll see neither one is being called or defined. Commented Jul 7, 2016 at 10:33

1 Answer 1

0

Sorry, i can't comment yet. I am watching this issue and tried a few things. Can you put a console log before post?

<script>
$('#referral-form').click(function (e) {
    e.preventDefault();
    var $form = $("#referral-form");


    if(!$form.hasClass("ajax"))
    {
        $form.addClass("ajax");
        $form.find('.error').css('display', 'none');

        console.log($form.serialize());

        var req = $.post('/reff/ref', $form.serialize());

        req.done(function(res) {
            $form.removeClass("ajax");

            if(res.success) {
                //do somthing
            }
            else {
                methods.printErrors(res.errors, $form);

                mixpanel.track('onReferralValidationFailure', {
                    errors: res.errors,
                    positionId: exported.position.id,
                    companyId: exported.position.company_id
                });
            }
        });

        req.fail(function() {
            $form.removeClass("ajax");

            mixpanel.track('onReferralUnknownError', {
                positionId: exported.position.id,
                companyId: exported.position.company_id
            });
        });
    }
});
</script>

Also, is any chance that your js function is not executed on click event?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for yoyr answer. My Js function is executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.