Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
112
rated 0 times [  117] [ 5]  / answers: 1 / hits: 5172  / 10 Years ago, tue, november 25, 2014, 12:00:00

So I know that when you put a URL in the action of an HTML form it will send the users to that url. I also added in the target attribute target=_blank to open the new URL in a new window. But I want to close that window and stay on the original site where the form was submitted form. I tried to do this by naming the target window and closing it but that didn't work out. I am wondering if there is a simple way to do this.



<form name=leds id=ledSend method=get target=newWindow action=https://agent.electricimp.com/NkzPvVKeHshT>
Lamp Control: <input type=radio name=led value=0 checked>Off
<input type=radio name=led value=1>On<br>
How long should the Lights stay on? <input type=text name=timer value=10>seconds<br>
Your name? For Our Records <input id=name type=text name=user placeholder=Your name here><br>
<input type=submit value=Update! onclick=alert(theInput.value +', You are about to change the Light! Keep in mind that there will be about a 2 second delay on the LiveStream.')/>
<script>
var theInput = document.getElementById(name);
</script>
</form>

<script>
newWindow.close();
</script>

More From » html

 Answers
14

Here is what you need, simply adjust it to your needs, create a php file that gets called within the action and let it do what you would do with your form - exactly the same way, but stay on the same page:



$(#ajaxform).submit(function(e)
{
var postData = $(this).serializeArray();
var formURL = $(this).attr(action);
$.ajax(
{
url : formURL,
type: POST,
data : postData,
success:function(data)
{
//data: return data from server
}

});
e.preventDefault(); //STOP default action
e.unbind(); //unbind. to stop multiple form submit.
});

$(#ajaxform).submit();





You need to include jQuery to your file to use this!






As you are using GET in your form - with this submission you will get the data in your $_POST.


[#41015] Tuesday, November 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tinakimv

Total Points: 126
Total Questions: 90
Total Answers: 104

Location: Netherlands
Member since Mon, Jun 7, 2021
3 Years ago
;