Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eliminate forms, do it with javascript

Status
Not open for further replies.

foxphpbomb

Programmer
Apr 10, 2007
29
US
With all this 2.0 stuff I'm wandering if there is a way to eliminate forms altogether and process form data with javascript?

Basically If I have a form on a div, and upon pressing a button is it possible to copy the values from the form fields and write them into another div?
 
I think it was possible before Web 2.0 whatever it's supposed to be but ... why would you want to have your data duplicated in two divs?

Cheers,
Dian
 
Of course that's possible, but I don't see how the form is processed like that. All the information will stay on the client's computer.

[monkey][snake] <.
 
well the idea, and maybe I wasn't being clear, is that the page won't need to be refreshed in order for the data to be processed.

I want to insert the data in a mysql db using php.

I would need the javascript to pass the values from the form fields to a php script, which would insert the values in the db.
 
That is good 'ol AJAX.

You can google AJAX, there are a lot of sites on it.

[monkey][snake] <.
 
Ok, but say I didn't want to use the XMLHttpRequest
object.

what I want to do is copy the values from the form into another div (id="2" for example). When this function is invoked and after the values are copied, the script dynamically includes the php insert script using document.write or something.

Is this possible? If it is could you provide some code on how
 
Server side processing occurs before any client side processing starts at all. So you really can't call the php insert script AFTER something takes place client side. The request to start the script has to be done server side, that is unless you use AJAX.



[monkey][snake] <.
 
it seems strange, because, if I dynamically include the php script on an onclick event or something it would seem to me that the parser, would read through it. I don't understand why this does not work. anyways...
 
The php script is executed on one computer called a web server. What it does is produce a stream of text. That stream is sent to another computer called a client where it is rendered by a program called a browser.

If the stream should happen to contain <SCRIPT> tags, the rendering, so to speak, is to treat that stream of text as a computer program to be executed on the client by, for example, the Javascript processor.

By "dynamically include", do you mean something like this
Code:
<html>
...
<div onclick="runPHP()">Click Me</div>
...
<script>
function runPHP() {
  <?php
     require 'do_some_php_stuff.php';
  ?>
}
</script>
<html>

If so, and if it worked, what happens is that on the web server the PHP processor spews forth the html up to the <?php ?> tags. Then executes do_some_php_stuff.php. Which in general outputs more html. And then sends the remaining html. What arrives at the browser is the html, not the php script.

When the div is clicked, the Javascript function runs. If the php script has produced valid Javascript, then more will happen inside the function; if not, then a Javascript error will occur and the function will stop.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top