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(),
));
}
}
$formhas all the correct data and$form.serialize()doesn't do anything funny to them.