Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Different Types of FORM Submission using JavaScript, CURL, FSOCKOPEN , AJAX  

Here i want to discuss about the different way to submit a form.

Traditional Form Submit:
This is the most common form uses everywhere.

< action="targetpage.php" name="myForm">
[form elements]
< type="submit" value="send" name="send">
< /form >

JavaScript Form Submit:

You can call a javascript function at any event for example onClick at button or OnChange at combo box and so on. And inside your javascript just write the line above.

document.myForm.submit();

CURL Form Submit:
This technique is rarely used in our application but we can submit any form using CURL just like normal form. Here is an example :

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url); // set the post-to url
curl_setopt ($ch, CURLOPT_HEADER, 1); // Header control
curl_setopt ($ch, CURLOPT_POST, 1); // tell it to make a POST, not a GET
curl_setopt ($ch, CURLOPT_POSTFIELDS, urlencode($queryString));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$response = curl_exec($ch);
curl_close ($ch);

FSOCKOPEN Form Submit:

This is also rarely used in our general application. For the Internet domain, it will open a TCP socket connection to hostname on port port. hostname may in this case be either a fully qualified domain name or an IP address. Here is a sample code:

$method =”GET” ; // POST or GET define here
$fp = fsockopen($host,80);
if ($method == ‘GET’)
$path .= ‘?’ . $data;
fputs($fp, “$method $path HTTP/1.1\n”);
fputs($fp, “Host: $host\n”);
fputs($fp, “Content-type: application/x-www-form-urlencoded\n”);
fputs($fp, “Content-length: ” . strlen($data) . “\n”);
fputs($fp, “User-Agent: MSIE\n”);
fputs($fp, “Connection: close\n\n”);
if ($method == ‘POST’)
fputs($fp, $data);

while (!feof($fp))
$buf .= fgets($fp,128);
fclose($fp);
return $buf;

AJAX Form Submit:

At any AJAX application we frequently use such code.

var http_request = false;
url ="myformactionpage.php";
parameters ="field1=val1&field2=val2";
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
` http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}

http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);

Share your thought if you find any others way to submit a html form.

Read More...

Advantages of JSP over ASP, SSI, JavaScript, HTML  

There are many advantages of using Java Server pages (JSP) over ASP, SSI, JavaScript, HTML. We shall look at some of them in detail.

  • vs. Active Server Pages (ASP). ASP is a similar technology from Microsoft. The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS-specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.
  • vs. Pure Servlets. JSP doesn't give you anything that you couldn't in principle do with a servlet. But it is more convenient to write (and to modify!) regular HTML than to have a zillion println statements that generate the HTML. Plus, by separating the look from the content you can put different people on different tasks: your Web page design experts can build the HTML, leaving places for your servlet programmers to insert the dynamic content.
  • vs. Server-Side Includes (SSI). SSI is a widely-supported technology for including externally-defined pieces into a static Web page. JSP is better because it lets you use servlets instead of a separate program to generate that dynamic part. Besides, SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.
  • vs. JavaScript. JavaScript can generate HTML dynamically on the client. This is a useful capability, but only handles situations where the dynamic information is based on the client's environment. With the exception of cookies, HTTP and form submission data is not available to JavaScript. And, since it runs on the client, JavaScript can't access server-side resources like databases, catalogs, pricing information, and the like.
  • vs. Static HTML. Regular HTML, of course, cannot contain dynamic information. JSP is so easy and convenient that it is quite feasible to augment HTML pages that only benefit marginally by the insertion of small amounts of dynamic data. Previously, the cost of using dynamic data would preclude its use in all but the most valuable instances.

Read More...