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".
Related
I have one text field with search button.
I want to access that text field's value on button click.
I can't use $_post because i have also one submit button inside.
So how to get that textfield's value on search button click.
my below's entire form is based on that unique value which was entered in that textdield
</head>
<script>
var getData = function()
{
var value = $("#inputData").val();
alert(value);
}
</script>
<body>
<form id="form1" name="form1" method="post" action="pag3.php">
<tr>
<td></td>
<td></td>
</tr>
//Other code for form which textfield should fill from data which match with
enterd value.
At end i have put submit button which will send data on next page form perform insertion
You can use jQuery as follow
Html
<input type="button" name="" id="inputData">
<button onclick="return getData()" >Get Data</button>
script
var getData = function()
{
var value = $("#inputData").val();
return false;
}
I have a form in HTML to apply a Discount Coupon to a current shopping cart.
I would like the user to just click on APPLY (after entering the coupon code) and then without refreshing the page, to have some PHP code run so it computes the corresponding discount.
Here is my form:
<form action="">
<input type="text" name="couponCode">
<input type="submit" value="Apply">
</form>
PHP to be run:
if (isset($_REQUEST['couponCode']) && $_REQUEST['couponCode']!='')
{
$couponCode = $_REQUEST['couponCode'];
if ($couponCode == "TEST1")
{
$discount=0.2;
}
}
How would this be done using javascript?
You need to use either the onsubmit event of the form or the onclick event of the button.
In the event handler, you assemble a URL and "get" it. For example:
<script type="text/JavaScript">
function submitCouponCode()
{
var textbox = document.getElementById("couponCode");
var url =
"https://www.example.com/script.php?couponCode=" + encodeURIComponent(textbox.value);
// get the URL
http = new XMLHttpRequest();
http.open("GET", url, true);
http.send(null);
// prevent form from submitting
return false;
}
</script>
<form action="" onsubmit="return submitCouponCode();">
<input type="text" id="couponCode">
<input type="submit" value="Apply">
</form>
Use jQuery AJAX. When it's complete, refresh your page as needed.
You can use Jquery to do an AJAX post you your PHP script, and then use JS to change the contents of the calling page.
http://api.jquery.com/jQuery.post/
It's simple with jQuery. You just have to use the right tag. If you use an "a" tag the page will refresh.
<button id="MyButton">Click Me!</button>
<script>
$("#MyButton").click( function(){
$.post("somefile.php");
});
</script>
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!'); })
}
}
});
});
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";
}
I have 2 buttons in HTML form. And both of them should submit the form. I would need to capture which button has been clicked so i can use it to perform different actions based on which button was clicked.
I am able to submit the form with both the buttons but how would i capture which button was clicked in the php file .
<INPUT type="button" name="submit" class="button" class="element_2" value="firstbutton">
<INPUT type="button" name="submit1" class="button" class="element_2" value="second button.. ">
i am using post method in Jquery to submit the form. How can i check which HTML button was clicked in server side php script
You could use a hidden element here, something like this:
<input type="hidden" id="submittedBy" name="submittedBy">
Your current .submit() handler using .serialize() and $.post() should work, just update the hidden field when clicking either button, for example:
$("form :submit").click(function() {
$("#submittedBy").val(this.name);
});
Then in PHP just check that $_POST["submittedBy"] == "submit" or "submit1" to see which caused it.
The alternative is to have the click handler POST and add in the value between .serializeArray() and when $.param() is called, like this:
$("form :submit").click(function() {
var data = $(this.form).serializeArray();
data.push({ name: 'submittedBy', value: this.name });
$.post("myPage.php", data, function(result) {
//do something
});
return false; //prevent normal form submission, even with the .submit() handler
});
Just do this,
HTML
<button type="submit" name="action[update]" value="1">Update</button>
<button type="submit" name="action[delete]" value="1">Delete</button>
PHP
$action = isset($_POST['action']['update']) ? 'update' : 'delete';
You CAN'T depend on JavaScript to tell you wich button was clicked, if user has JavaScript disabled, your form is broken.
You could try using the isset function
if(isset($_POST['submit'])){
echo "first button was clicked";
}
Or to detect the second one was clicked:
if(isset($_POST['submit1'])){
echo "second button.. was clicked";
}
I would capture all the events on the page using:
$(document).click(function (obj) {
if ('equipmentSetup' === obj.target.id) {
$('#form').submit();
}
....
Then add an if statement looking for the "id" of the button you want. Don't use the name or id "submit". I forgot why but I remember it caused problem for me.
You could try changing the buttons to type="button" and give them ids. With that, you can use the jquery line (not able to check my syntax below, but think i've got it right)
$('form button').click(function(){
if($(this).attr('id') = "button1"){ ...button 1 clicked}
..process form here..
});
would that do it?