Reputation: 109
first time posting :)
I am doing an exercise in Ruby and i am a little stuck with the CGI method.
I used the HTML code below to send the form's input.
Bit from HTML
<form action="afficher.cgi" method="post">
<label> Votre prenom </label> : <input type="text" name="prenom"/> <br>
<label> Votre nom </label> : <input type="text" name="nom"/> <br>
<label> Votre nom </label> : <input type="text" name="age"/> <br>
<input type="submit" value="Valider" />
</form>
When I submit the form, it goes to the .cgi page (executing the script) and works.
I was wondering if it possible to send the information to the .cgi and refresh the .rhtml and not going to the .cgi and be stuck there.
I tried to find a method or somethings to write in the .cgi that redirects to the .rhtml, but no clue. Is there a solution inside the .rhtml document ?
Thanks !
Upvotes: 2
Views: 135
Reputation: 2053
You would probably have to use javascript/ajax for that.
In your form
tag, you should specify a function for the onsubmit
field, instead of specifying a destination in action
.
<form onsubmit="return submitForm(this)" id="form1" action="" method="post">
And then, in your javascript,
function submitForm()
{
var xmlhttp= window.XMLHttpRequest ?
new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
alert(xmlhttp.responseText); // Here is the response
}
var formData = new FormData( document.getElementById("form1") );
xmlhttp.open("POST","afficher.cgi", true);
xmlhttp.send(formData);
return false;
}
Upvotes: 1