Showing posts with label form. Show all posts
Showing posts with label form. Show all posts

Tuesday, August 7, 2007

How to auto-submit a form

Sometimes it's useful to pass data to a hidden page that processes the data then passes the new data to another page. To pass the data through forms, use javascript. Set a body onload function with $("form_name").submit() to submit the form when the page finishes loading.

Example:

<?php
....data processing code here
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="javascripts/prototype.js"></script>
</head>
<body onload="$('newdata').submit()">
<form id="newdata" name="newdata" method="post" action="newviewer.php">
<input type="hidden" id="data1" name="data1" value="x1">
</form>
</body>
</html>

JSON, php, and html forms

So I've been working on a data processing page that is hidden from the user, takes form data from an input page, processes the data, encodes the data using json in a new form, then auto submits the form to a new page. Here are a few helpful functions that got it working properly:

1. The json_encode() function in php doesn't escape some html special characters like quotes. It's helpful to wrap it with the function htmlspecialchars() so that it plays nice with your html.

2. php line breaks are '\r'. I had inadvertently included some line breaks in my string variables. To fix that problem use the trim() function.