0

I am trying to learn jQuery. Working on an employee website that will allow entry via a unique keypad code for each employee. Anyways, came across this script and am having a hell of a time getting it to function as I would expect.

Here is my form:

<form name='keypad_test' class='form-inline' method='post' role="form" >
    <div class='form-group'>
        <label for='user_id'></label>
        <div class='col-sm-10'>
            <input  type='password' id='user_id' name='user_id' class='keypad' disabled='disabled' />
        </div>
    </div>

Here is how I am being forced to get the variable. I want to use serialization for the data, but I can't get it to work to save my life.

<script type="text/javascript">
    $('form').submit(function(e) {
        var user_id_var = $('input.keypad').val();
        $.post("process.php", { user_id:user_id_var }, function(data) {
            alert(data);
        });

process.php is just $user_id = $_POST["user_id"]; echo $user_id;

Is it possible for someone to tell me what I need to do to serialize the data so I know it is clean? My solution so far was taking bits from different answers on this post here.

Community
  • 1
  • 1

1 Answers1

0

I see that you're using both PHP and JavaScript (and jQuery). I think the problem you're having is that you want to apply PHP serialize() to some text, but in JavaScript. Like this:

s:5:"Hello";

The problem is that jQuery uses the term 'serialize' to describe compressing the contents of an entire form, into URL encoded text, like this:

a=10&b=apple // field 'a' has a value of 10, field 'b' has a value of 'apple'

It makes me think you're referring to the PHP serialization, which can be used to truth-check that a variable's content is what it says it is.

There is no such function in jQuery. However PHPJS have made a collection of JavaScript functions which act like the PHP equivalent. Here's one for serialize()

Here's a working JSFiddle based on your code. I've removed the .post() functionality because on JSFiddle there is no server side script to respond. Just enter some text in the box and press Enter to submit the form. The alert will display the PHP serialize() version of the string.

1owk3y
  • 1,115
  • 1
  • 15
  • 30
  • Sorry I wasn't clear. I was trying to use $form.serialize() via jQuery as you mention, but honestly, anything I can use that works with a little more security than my version. Coding obviously isn't my day job so sometimes it's hard for me to wrap my head around what I really want/need to do. – Joshua Guiher Mar 15 '15 at 17:37