I have a problem, i want to print a a div after form validating and then submitting. Issue is, when call print.window function in submit button, it display print window without validating form fields.
input type="submit" value="Submit" class="button" name="submit" onclick="window.print()">
while the div which to be print is in
if($_POST['submit'])
{
echo"print div here";
}
Please help.
Thanks
If you want to print something after form validating and then submitting, you are supposed to do it in client side.
You can do something like this:
<form name="myForm" action="action.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
Then, validate form like this and print.
<script>
function validateForm() {
// validate your form here
window.print();
}
</script>
Hope it helps.
Related
I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks
The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.
Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...
I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you
Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}
Before my HTML form submission, I am putting some values in my hidden input element, which is not submitted with my form, any error or something?
For ex:
Below is my form function:
<form id="clientForm" name="clientForm" method="post" enctype="multipart/form-data" action="clients.php" onClick="beforeSubmit(); return false;">
<input type="text" name="OLD_BRANCH_FILTERS_ALL" id="OLD_BRANCH_FILTERS_ALL" value="">
</form>
Below is my JS function:
<script language="javascript" type="text/javascript">
function beforeSubmit(){
$('#OLD_BRANCH_FILTERS_ALL').attr('value', 'xyz');
alert(document.getElementById('OLD_BRANCH_FILTERS_ALL').value);
document.clientForm.action = 'clients.php?saveBtn=Save';
document.clientForm.submit();
}
</script>
Now, When I check the form request $_REQUEST in PHP, it shows no value in element.
Any clue why its not working.
Thanks in advance !
your onlick is not called in time to set the values, try it this way
<form id="clientForm" name="clientForm" method="post" enctype="multipart/form-data" action="clients.php">
<input type="text" name="OLD_BRANCH_FILTERS_ALL" id="OLD_BRANCH_FILTERS_ALL" value="">
</form>
and the jquery
$('#clientform').submit(function() {
beforeSubmit();
});
Also, if you also wanted to provide validation to the field you could use,
$('#clientform').submit(function(e) {
beforeSubmit();
if (!{VALID}) {
e.preventDefault();
}
});
I'm trying to work with ajax. I have two pages: request.html and reply.php.
request.html:
<html>
<script language="javascript">
var xht = new XMLHttpRequest();
function testAJAX()
{
xht.open("get","http://localhost:9999//a.php", true);
xht.send();
xht.onreadystatechange=function() {
if (xht.readyState==4) {
alert("Text: "+xht.responseText);
}
}
}
</script>
<form id="form1" name="form1" method="post" action="">
btn
<input name="btn" type="submit" id="btn" onClick="testAJAX();" value="Submit" />
</form>
</html>
reply.php:
<?php
echo 'hi';
?>
The problem is that I don't get a response via xht.responseText and with xht.responseXML I get null and with xht.status I get 0.
I asked the link http://localhost:9999//a.php via browser and got hi correctly.
P.S: I tried this on Chrome 29.0.1547.18 and Maxthon v4.1.1
any ideas..
You don't need to mention "http://localhost".
The main mistake is you have given the input type as Submit If it is submit the form will be submitted first the click event will not trigger. Change the input type to button.
If you want to do form submission do it in java script
The corrected code is below.
<form id="form1" name="form1" method="post" action="">
btn
<input name="btn" type="button" id="btn" onClick="testAJAX();" value="Submit" />
// change type to button
</form>
var xht = new XMLHttpRequest();
function testAJAX()
{
xht.open("get","a.php", true); /// Change to a.php
xht.send();
xht.onreadystatechange=function() {
if (xht.readyState==4) {
alert("Text: "+xht.responseText);
}
}
}
Adding to SarathPrakash's answer, I would like to point out that there is nothing wrong with specifying localhost. It will still work as long as the PHP file's address is valid.
You can also have the submit button. But you'll have to modify the form opening tag as follows:-
<form id="form1" name="form1" method="POST" action="" onsubmit="return false">
This is will stop the default behaviour of the form being submitted. Although in my opinion, it is best to avoid it altogether, and just stick with assigning the correct event handler to the onclick attribute.
Also, it is good practice to follow the correct syntax for HTML documents.
<html>
<head>
<title> Your title here </title>
<script type="text/javascript"> Your script here </script>
</head>
<body>
Your main document text here. Forms, tables etc.
</body>
</html>
For a simple tutorial, you could try this.
I have a form that I need to submit automatically... (the fields are already filled and its complicated to explain why but it IS necessary to do it this way)..
I know how to autosubmit the form using Javascript but the only problem I have is that there is more than 1 submit button.. and I need 1 in particular to be submitted...
thanks in advance
EDIT2(source):
<I put the javascript in the head... />
<FORM ACTION="PDF.php" name="form" METHOD="post">
<A whole bunch of inputs />
<INPUT TYPE="submit" name="form-save" VALUE="Save Changes" >
<INPUT TYPE="submit" name="form-submit" VALUE="Submit" >
<input type="submit" name="print" id="print" value="Download PDF" />
</form>
instead of going for a click event on a submit button, you can call submit of a form object from javascript.
Example :
<head>
<title>Auto Submit Form</title>
<script type="text/javascript">
window.onload = function () {
var form = document.getElementById("PDFGenerationForm");
form.submit();
};
function OnFormSubmit() {
alert("Submitting form.");
}
</script>
<body>
<form id="PDFGenerationForm" action="" method="post" onsubmit="OnFormSubmit">
<!--Any input tags go in here-->
</form>
This editor won't let me paste the whole HTML in here. So, it is in fragments.
$("#yourbuttonid").click();
EDIT:
<form>
...
<input type="submit" id="myFirstsubmit" />
<input type="submit" id="mysubmit" />
</form>
<script type="text/javascript">
$(document).ready(function(){$("#mysubmit").click();});
</script>
If you really want to click a specific button, add this script to the end of your page:
<script type="text/javascript">
// press the button
var myButton = document.getElementById("idOfTheButtonToClick");
myButton.click();
</script>
This assumes your button has an ID.
1) Here is a working auto-submit method: when page is loaded, the form will be immediately autosubmited (the values can be set with php variables too.)
<form action="page.php" name="FORM_NAME" method="post">
<input type="text" name="example1" value="YOUR_VALUE" />
<input type="submit" />
</form>
<SCRIPT TYPE="text/JavaScript">document.forms["FORM_NAME"].submit();</SCRIPT>
or use for any form on that page:
document.forms[0].submit();
2) you can use button-click (called after 1 second):
<SCRIPT TYPE="text/JavaScript">setInterval(function () {document.getElementById("myButtonId").click();}, 1000);</SCRIPT>
I need jquery to check if my posted filename (up_image) is empty or not.
if it's empty i need a div tag to be shown and come with some kind of alert message.
if not, just do the
$("#submit").submit();
<form action="/profile/" method="post" enctype="multipart/form-data" id="submit">
<p>
<label for="up_image">image:</label>
<input type="file" name="up_image" id="up_image" />
</p>
Upload
</form>
$(function() {
$("#post_submit").click(function() {
var fil = $("#up_image");
if($.trim(fil.val()).length == 0) {
alert("Choose a file!");
fil.focus();
return false;
}
$("#submit").submit();
});
});
1: use a standard submit button to submit your form rather than a javascript-dependent link, for accessibility reasons, and to prevent brokenness if someone tries to right-click-open-in-new-window or other similar action on the link. If you want it to look like a link, you can still use a button, just apply some CSS to make it no longer look like a button.
2: use the form.onsubmit event to do validation rather than relying on a submit button click (forms can also be submitted by pressing enter, which may not always generate a button click)
<form id="uploadform" method="post" action="/profile/" enctype="multipart/form-data">
<p>
<label for="up_image">image:</label>
<input id="up_image" type="file" name="up_image" />
</p>
<p>
<input type="submit" value="Upload" />
</p>
</form>
<script type="text/javascript">
$('#uploadform').submit(function(e) {
if ($('#up_image').val()=='') {
alert('Please choose a file to upload.');
e.preventDefault();
}
});
</script>