Perform 2 Actions On Button Click Form - php

My Requirement
Form Submission in New Window & Redirect Existing Page to a new page
on same click.
When I tried only Form submission is happening. Javascript function to redirect is not working.
Html
<form id="feedback.php" name="form1" method="post" action="feedback.php" target="_blank">
<input type="submit" class="button" value="SUMBIT" onclick="btntest_onclick();"/>
</form>
Javascript
<script>
function btntest_onclick()
{
window.location.href("mainpage.php");
}
</script>
Actual
But Only Form Submission is Happening in New Window. Existing Page is
not redirected.
Please tell me how to do this ?

Your HTML :
<form id="feedback.php" name="form1" method="post" action="feedback.php" target="_blank">
<input type="submit" class="button" value="SUMBIT" onClick="btntest_onclick()"/>
</form>
See onClick
Your Javascript :
function btntest_onclick()
{
setTimeout(function(){
window.location.href = "mainpage.php";
},0);
}
window.location.href is not a function.
And apparently it needs to be async to work.
JsFiddle

add onsubmit to your form.
and inside function do something like that:
function btntest_onclick() {
window.open("http:// your url");
window.location.href="/mainpage.php";
}

Related

How do I send input to url using form without Php? (form action)

I want to send this query (EA452401760IN) to https://track.aftership.com/india-post/.
After a successful submission by Form the URL will be
https://track.aftership.com/india-post/EA452401760IN
Form
<form method="POST" action="https://track.aftership.com/india-post/" target="_blank"> <input type="text" value="EA452401760IN"></input><button type="submit"> submitc</button></form>
Do I need a PHP file for this?
If yes, how do I write a php file for this problem.
Pure Javascript approach with Jquery
<input type="text" id="trackingNumber" value="EA452401760IN"></input>
<button onclick="go()">Track</button>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script>
$( "button" ).on( "click", function( event ) {
window.location.href = "https://track.aftership.com/india-post/" + $('#trackingNumber').val();
});
</script>
JSBIN: https://jsbin.com/fuqovocoza/edit?html,console,output
<?php if(($_SERVER["REQUEST_METHOD"] == "POST")){
//Check that the form has been posted if it has been then redirect to the correct page
header ('Location: https://track.aftership.com/india-post/' . $_POST['query']);
}
else
{
if the form has not been submitted display the form
<form method="POST" action="https://track.aftership.com/india-post/" target="_blank"> <input type="text" name="query" value="EA452401760IN"></input><button type="submit"> submitc</button></form>
Note that I have changed the name of the input field to query.

Run a PHP script on click of a button?

I have a php file which I want be executed when I click on a button. I have been using the following code to achieve the same:
<form action="test.php" method="post">
<input type="submit" value="Run Script" name="submit">
</form>
The code seems to take me to test.php file, but I want to be redirected back to the page where I was. Can is be possible to run this test.php on click on a button and not get redirected? Something like running the script in background?
Please let me know if you guys need further clarification.
Any help will be highly appreciated.
Thanks in advance guys!
you will need to use AJAX
<script>
function doTheFunction(){
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","URL_OF_PHP_FILE",true);
xmlhttp.send();
}
</script>
<button onclick"doTheFunction();">Run the script</button>
You need to do an ajax call and prevent the forms submit.
Html:
<form onsubmit="return doAxajCall();"> </form>
js Code:
function doAjaxCall(){
//do ajax call however you want
return false; //prevents the form from submitting
}
Easy way;
Make the action of your form 'self' like this;
<form action="#" method="post">
<input type="submit" name="submit" value="Do PHP">
</form>
Then test for submission at the top of the page like this;
if (isset($_POST['submit'])) {
// do stuff
}
in your instance 'test.php' has to included in the original file.
at the bottom of you php file
header('Location: lastPage.php');
or do some tricks using ajax.
code:
<!-- including jquery.min.js cdn -->
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<input type="submit" value="Run Script" id="submit" name="submit" />
<!-- now set ajax call for button -->
<script>
$("#submit").click(function(){
$.ajax({
url:'test.php',
type:'POST',
success:function(response){
alert('test.php file executed');
}
});
});
</script>

HTML form submit show javascript confirm window

I have a HTML form:
<form method="post" action="addticketupdate.php">
....
</form>
Then above the form I have:
<?php
if(isset($_POST["submit"])) {
... do stuff here
}
?>
So the submit button in my form has an id and name of "submit"
I want to make a JavaScript confirm box popup when the submit button is clicked, the statement says: If user clicks "OK" the php code excuted, else do nothing.
How can I do that?
Simply use this:
<form method="post" action="addticketupdate.php" onsubmit="return confirm('Do you want to submit the form?');" >
Option 2: (On submit button click)
$('#submit').click(function() {
var res = confirm('Do you really want to submit the form?');
if(!res){
return false;
}else{
//submits form
}
});
You can do as,
<form method="post" action="addticketupdate.php" onsubmit="return confirm('Need to submit??');">
<!-- your other html input code -->
<button type="submit">submit</button>
</form>
<script>
function validate() {
var result = confirm("Do you want to submit!");
return result;
}
</script>
<form name="form-name" onsubmit="return validate();">
That should be making using dialog box instead alert popup box.
However dialog don't have full support so "Fancy Box" is the best in this case.
FancyBox
So after installizing fancy box into your website you should remove the default button they have there and change it to input type submit and add name it as "submit".

SimpleModal form submit

i have a php page "formpage.php", in there is an form like this:
<div id='hiddenform' style='display:hidden'>
<form name="testform" action="formpage.php" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
</div>
this form displayed with jquery and simple modal (example):
$(".show_hide").click(function(){
$('#hiddenform').modal({overlayClose:true});
});
all works fine, the box open with SimpleModal and the form is displayed, but i cant submit the form, when i press the submit button nothing happens. what should i do? the form and the submit works fine without SimpleModal.
i want submit the form (open with SimpleModal) to formpage.phhp (self) and then i use the posted variables further in the script.
thank you for your help!
Does this work? If it does something in your js is preventing the default behaviour of the form(might be simplemodal might be something else):
$(".show_hide").click(function(){
$('#hiddenform').modal({
overlayClose:true,
onShow: function() {
$('.simplemodal-data input[type=submit]').click(function() {
$.post('the_url_where_you_want_to_send_your_data_to', $(this).closest('form').serialize(), function(data) { alert('we have a response and form has been sent!'); })
}
}
});
});

Jquery form submit after confirn with setting submit button

I am using JQuery and SimpleModal Confirm Modal Dialog to show a confirm box before uploading. The form is submitting fine but as i'm checking isset submit button in PHP therefore it fails. How could i submit post using JQuery with setting submit button.
Here is the code
HTML
<form enctype="multipart/form-data" action="#duc" method="post" id="uploadform" >
<input name="file" type="file" >
<input name="Submit_upload" type="submit" value="Upload" id="Upload" >
</form>
JQuery
jQuery(function ($) {
$('#Upload').click(function (e) {
e.preventDefault();
confirm("Continue to the Upload?", function () {
$('#uploadform').submit();
});
});
})
PHP
if(isset($_POST['Submit_upload']) && $_FILES['file']['name'])
{
// file uploading process.
}
I think as after confirm JQuery submitting the form so the actual submit button is not adding in the post array. any help will be much appreciated.
When you submit the form via .submit, type=submit input values are not sent at the same time. There are a ton of different ways to solve/handle this. One is to do:
<input name="Submit_upload" type="hidden" value="true">
<input type="submit" value="Upload" id="Upload">
The hidden input will be sent.
I have tried your code and it is working fine man
i just use one page to use your code. on top i put your PHP code then jquery code in head after jquery.in and then html part and its working fine why it is not working in your machine might you clean cache or browser;
here is my code
<?php
if(isset($_POST['Submit_upload']) && $_FILES['file']['name'])
{
echo $_POST['Submit_upload'];die;
}
?>
<html>
<head>
<script src="jquery.min.js" type="text/javascript"></script>
<script>
jQuery(function ($) {
$('#Upload').click(function (e) {
e.preventDefault();
confirm("Continue to the Upload?", function () {
$('#uploadform').submit();
});
});
})
</script>
</head>
<body>
<form enctype="multipart/form-data" action="#duc" method="post" id="uploadform" >
<input name="file" type="file" >
<input name="Submit_upload" type="submit" value="Upload" id="Upload" >
</form>
</body>
</html>

Categories