Jump to content

jquery serialization of form


Q695

Recommended Posts

Working from this code:

https://github.com/FREE-FROM-CMS/form/blob/master/Handler/engine.js

function process_form(page_in, page_out){
$("form#register").submit(function() {
    var mydata = $("form#register").serialize();
    alert(mydata); // it's only for test
    $.ajax({
        type: "POST",
        url: page_in,
        data: mydata,
        success: function(response, textStatus, xhr) {
            alert(page_out);
        },
        error: function(xhr, textStatus, errorThrown) {
            alert("error");
        }
    });
    return false;
});
}

How do I pass all the data to my generic data receiver at page_in taken from:
https://github.com/FREE-FROM-CMS/form/blob/master/Handler/register.html

<script type="text/javascript" src="forms\engine.js"></script>
<form id='register'>
User ID:<br>
<input type='text' name='user' placeholder='User ID'><br>
E-mail Address:<br>
<input type='email' name='email' placeholder='E-mail Adress'><br>
Password:<br>
<input type='password' name='pw1' placeholder='Password'><br>
Password Verification:<br>
<input type='password' name='pw2' placeholder='Password Verification'><br>
<input type='button' value='Register' onclick='process_form(\"testing/data_passing.php\", \"forms/login.php\")'>
</form>

No data seems to be sent to the server, and it doesn't seem to ge triggering a generic receiver also as shown here:
https://github.com/FREE-FROM-CMS/form/blob/master/Handler/php/data_passing.php

<?php
ob_start();// start creating data object to be inserted to client
echo"GET:";
print_r($_GET);
echo"
<p>POST:";
print_r($_POST);
$output = ob_get_contents();//sets data output for database
ob_end_clean();
require_once"../secure/db_conn.php";
$sql="INSERT INTO  `database`.`test`
(`data`)
VALUES (:data);";
$q = $conn->prepare($sql);
$q->execute(array(':data'=>$output));

When I just load the page part 3 does work generically, or with data.

Link to comment
https://forums.phpfreaks.com/topic/296276-jquery-serialization-of-form/
Share on other sites

Take a look at this, might lead you somewhere. http://stackoverflow.com/questions/12719859/no-visible-cause-for-unexpected-token-illegal

This line I would personally use

var mydata = $(this).serialize();
// OR
var mydata = $(this).serializeArray();

I recently found the serializeArray() to be more handy, especially when you need/want to add hidden values to the posting array, like 'ajax' => true.  So then the receiving php can know if the form was posted via ajax or not.  Yes I know it's not foolproof on that, so I always make my form work with or without ajax.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.