PDA

View Full Version : help creating a form with JavaScript/PHP



null-cipher
March 6th, 2009, 12:30 PM
Hi Ubuntu forums!
I'm working on a website for a school project, it's quite flash with PHP making everything dynamic but I need a spot of help with JavaScript.

I'm trying to make a form (a table of sorts) that is generated by another form (specifying columns and rows).

I know I could do this in PHP by POSTing or GETting the values I need to another script, but I would be interested in learning how to do this in the same page. I would assume I would need to be using some kind of AJAX to rig this up.

Now I am fairly familiar with XHTML, CSS, and PHP, but I have very little knowledge of JavaScript at all.

I would really appreciate any help! Thanks.

simeon87
March 6th, 2009, 01:07 PM
You can simply use


document.write('<p>This is a paragraph.</p>');

to write content to the document. You can indeed use Ajax to query the webserver for data and then process that data using your Ajax framework and then write it to the document.

jackmcslay
March 6th, 2009, 02:46 PM
document.write is deprecated, and all it does is appending content to the page's HTML source, which is very cumbersome

to properly insert data, use document.getElementById(id).innerHTML:


<html>
<head>
<title>Javascript test</title>
</head>
<body>
<div id="content"></div>
<script type="application/x-javascript">
document.getElementById('content').innerHTML = 'Random number: ' + Math.random();

</script>
</body>
</html>


also, I find appending dinamically-generated javascript files easier than using ajax requests:


function appendJS(jsfile){
var obj = createElement('script');
obj.type = 'application/x-javascript';
obj.src = jsfile;
document.getElementsByTagName('body')[0].appendChild(obj);
}

drubin
March 6th, 2009, 07:36 PM
I would suggest you use Jquery (http://jquery.com) as a Java Script libary.

There are just far far to many web browsers to not be using some sort of framework to hide the abstractions each web browser provide.

I used to suggest starting from the bottom up but I have changed my mind since learning document.write is useless when you are just using a differnt API call but you know this one is valid on all platforms.

Jquery has tones of great Ajax Api's as well as Animations.