I am trying to submit a form when the page loads. I have tried quite a few different things and none of them seem to work. If I use
window.onload = function(){
alert("test");
}
It works fine but when I try to use
window.onload = function(){
console.log($("#testForm")[0].submit), 2000);
}
or
window.onload = function(){
document.getElementById("testForm").submit();
}
It no longer works.
JS:
window.onload = function(){
document.getElementById("testForm").submit();
}
HTML:
<form name="testForm" id="testForm" method="POST" action="test.php">
<input name="MERGE1" type="text" id="fname" size="40" maxlength="40" value="<?php echo $fname; ?>" />
<input name="MERGE2" type="text" id="lname" size="40" maxlength="40" value="<?php echo $lname; ?>" />
<input name="MERGE0" type="text" id="email" size="40" maxlength="40" value="<?php echo $email; ?>" />
<input name="MERGE3" type="text" id="company" size="40" maxlength="40" value="<?php echo $company; ?>" />
<input name="MERGE6" type="text" id="phone" size="40" maxlength="40" value="<?php echo $phone; ?>" />
<input name="MERGE19" type="text" id="city" size="40" maxlength="40" value="<?php echo $city; ?>" />
<input name="Continue" type="submit" value="Continue" />
</form>
Try this:
$("#testForm").submit()
I replaced the POST with a GET and the action with a working URL so you can see something happening.
Working fiddle
Related
Assume that I have a PHP page that has three forms.
Form one, it will ask the user to enter the email address while the code is hidden.
<label for="email">Email Address:</label> <input type="text" id="email" name="email">
<input type="hidden" id="code" name="code" value="42"> <input type="submit" value="Submit1">
Form two will be showing after step 1. In this step, the user will be asked to enter the code in step 1 (which is equal to 42).
<input type="number" id="code2" name="code2"> <input type="submit" value="Submit2">
Form three, will be showing after entering corrected code in step 2.
<input type="number" id="code2" name="code2"> <input type="submit" value="Submit3">
How can I program it?
You could try this approach :
<?php
function getFormContent() {
$email = $_POST['email'] ?? null; //$email equals to null if it is undefined, to prevent errors
if (!$email) {
echo '<input type="text" name="email" placeholder="Email.." />';
echo '<input type="hidden" id="code" name="code" value="42">';
} else {
echo '<input type="hidden" name="email" value="'.$email.'" />'; //keep email set after submitting first step
}
if ($email && $_POST['code']) {
if (isset($_POST['code2']) && $_POST['code2'] == $_POST['code']) {
//final form
}
else { //second step
echo '<input type="number" id="code2" name="code2">';
echo '<input type="hidden" id="code" name="code" value="42">'; //we still need this in case user submitted a wrong code
}
}
}
?>
<form method="post">
<?= getFormContent() ?>
<input type="submit" />
</form>
You need to state the action of the form as the name of the file.
So, if the file you are working on is name example.php it should look like so:
$email = trim(get_variable_value('email'));
$code = trim(get_variable_value('code'));
$code2 = trim(get_variable_value('code2'));
//make sure to validate the input!! if it's not valid send back
<? if(!isset($email)){ //alternatively, you can add a hidden input `stage`. I just don't like it, as it can be bypassed.
?>
<form name="transferToNewForm" action="example.php" method="post">
<label for="email">Email Address:</label> <input type="text" id="email" name="email">
<input type="hidden" id="code" name="code" value="42">
<input type="submit" value="Submit1">
</form>
<? }
if(isset($email) && isset($code) && $code == 42){ ?>
<form name="transferToNewerForm" action="example.php" method="post">
<label for="email">Email Address:</label> <input type="text" id="email" name="email" value="<?= $email ?>" readonly>
<input type="number" id="code2" name="code2">
<input type="submit" value="Submit2">
</form>
<? }
if(isset($email) && isset($code2) && $code2 == 42){ ?>
<form name="finalFrom" action="theActualPageYouWant.php" method="post">
<label for="email">Email Address:</label> <input type="text" id="email" name="email" value="<?= $email ?>" readonly>
<input type="number" id="code2" name="code2">
<input type="submit" value="Submit3">
</form>
<? } ?>
I have a situation where I want to auto submit form in cakephp ctp file. Can anyone please help me? I know there are number of same or similar question have asked before this. It tried everything, but it doesn't work. Please have a look on my code:
SCRIPT PART:
<script>
window.onload = function() {
alert("reached here");
document.frmTransaction.submit();
}
</script>
HTML PART:
<form method="post" action="https://secure.ebs.in/pg/ma/sale/pay" name="frmTransaction" id="frmTransaction" onSubmit="return validate()" onLoad="javascript:autoSubmit();">
<input name="account_id" type="hidden" value="<?echo $_POST['account_id'] ?>" />
<input name="return_url" type="hidden" size="60" value="<?echo $_POST['return_url'] ?>" />
<input name="mode" type="hidden" size="60" value="<? echo $_POST['mode']?>" />
<table style="width:50%">
<tr>
<td>Reference: </td><td><input name="reference_no" type="text" value="<? echo $_POST['data']['reference_no'] ?>" readonly/></td>
</tr>
<tr>
<td>Amount: </td><td><input name="amount" type="text" value="<? echo $_POST['data']['amount'] ?>" readonly/>Rs.</td>
</tr>
</table>
<input name="description" type="hidden" value="<? echo $_POST['description'] ?>" />
<input name="name" type="hidden" value="<? echo $_POST['name'] ?>" />
<input name="email" type="hidden" value="<? echo $_POST['email'] ?>" />
<input name="phone" type="hidden" value="<? echo $_POST['phone'] ?>" />
<input name="address" type="hidden" value="<? echo $_POST['address'] ?>" />
<input name="country" type="hidden" value="IND" />
<input name="postal_code" type="hidden" value="<? echo $_POST['postal'] ?>" />
<input name="state" type="hidden" value="<? echo $_POST['state'] ?>" />
<input name="city" type="hidden" value="<? echo $_POST['city'] ?>" />
<input name="secure_hash" type="hidden" size="60" value="<? echo $secure_hash;?>" />
<br/><br/><br/><input name="submit" value="Submit" type="submit" id="submit" style="background:#ff6600;color:#fff;height:40px;width:150px;font-family:Calibri;font-size: 22px;"/>
</form>
You have to check form definition again.
Remove onload event statement.
<form method="post" action="https://secure.ebs.in/pg/ma/sale/pay" name="frmTransaction" id="frmTransaction" onSubmit="return validate()">
I have a javascript function named formApprove() and am calling that function within html onclick on a button. For some reason the javascript function is not being recognized because firebug tells me that the forApprove is not a function. Below is the code in the head:
<script type="text/javascript">
function formApprove() {
var TheTextBox = document.getElementById("formApprove");
TheTextBox.value = 'true';
alert("hellp");
return true;
}
</script>
Below is the code in the body:
<input type="submit" name="approve" value="Approve Membership" onclick="formApprove()" />
Any help on this would be greatly appreciated.
Full Code:
<head>
<script type="text/javascript">
function nextPrevious(txtElement, btnElement) {
switch(btnElement.name) {
case 'increase':
var numrows = document.getElementById("numOfRows");
if (txtElement.value < Number(numrows.value)) {
txtElement.value++;
} else {
alert('You are alread at the end of the members');
return false;
}
break;
case 'decrease':
if(txtElement.value > 1) {
txtElement.value--;
} else {
alert('You are alread at the begining of the members');
return false;
}
break;
case 'goto':
alert(txtElement.value);
break;
default:
}
return true;
}
function approve() {
var TheTextBox = document.getElementById("formApprove");
TheTextBox.value = 'true';
alert("hellp");
return true;
}
function formChange() {
var TheTextBox = document.getElementById("formSubmit");
TheTextBox.value = 'true';
return true;
}
function deleteRecord() {
var answer = confirm ("Are you sure you want to delete record?");
if (answer) {
var TheTextBox = document.getElementById("formDelete");
TheTextBox.value = 'true';
return true;
} else {return false;}
}
</script>
</head>
<body>
<form name="members_view" method="POST" action="viewpending.php">
<br />
<div id="buttons">
<input type="submit" name="update" value="Update Record" onclick="formChange(this.form.elements['formSubmit'].value)" /> -
<input type="submit" name="decrease" value="Previous" onclick="nextPrevious(this.form.elements['record'], this)" /> -
<input type="submit" name="increase" value="Next" onclick="nextPrevious(this.form.elements['record'], this)" /> -
<input type="submit" name="delete" value="Delete Record" onclick="deleteRecord(this.form.elements['formSubmit'].value)" /> -
<input type="submit" name="approve" value="Approve Membership" onclick="approve()" />
</div>
</div>
<div id="leftcase">
<div class="label">First Name:</div><div class="input"><input size="40" type="text" name="FNAME" value="<?php echo $row['FNAME']; ?>" /></div>
<div class="label">Middle Name:</div><div class="input"><input size="40" type="text" name="MNAME" value="<?php echo $row['MNAME']; ?>" /></div>
<div class="label">Last Name:</div><div class="input"><input size="40" type="text" name="LNAME" value="<?php echo $row['LNAME']; ?>" /></div>
<div class="label">Full Name:</div><div class="input"><input size="40" type="text" name="FULL_NAME" value="<?php echo $row['FNAME'].' '.$row['MNAME'].' '.$row['LNAME']; ?>" /></div>
<div class="label">Nick Name:</div><div class="input"><input size="40" type="text" name="NNAME" value="<?php echo $row['NNAME']; ?>" /></div>
<div class="label">Spouse/Partner Name:</div><div class="input"><input size="40" type="text" name="H_COUNTRY" value="<?php echo $row['H_COUNTRY']; ?>" /></div>
<div class="label">Anniversary Date:</div><div class="input"><input size="40" type="text" name="B_ZIP" value="<?php echo $row['B_ZIP']; ?>" /></div>
<div class="label">Primary Email:</div><div class="input"><input size="40" type="text" name="EMAIL" value="<?php echo $row['EMAIL']; ?>" /></div>
<div class="label">Second Email:</div><div class="input"><input size="40" type="text" name="B_CITY" value="<?php echo $row['B_CITY']; ?>" /></div>
<div class="label">Home Street:</div><div class="input"><input size="40" type="text" name="H_STREET" value="<?php echo $row['H_STREET']; ?>" /></div>
<div class="label">Home City:</div><div class="input"><input size="40" type="text" name="H_CITY" value="<?php echo $row['H_CITY']; ?>" /></div>
<div class="label">Home State:</div><div class="input"><input size="40" type="text" name="H_STATE" value="<?php echo $row['H_STATE']; ?>" /></div>
<div class="label">Home Zip:</div><div class="input"><input size="40" type="text" name="H_ZIP" value="<?php echo $row['H_ZIP']; ?>" /></div>
<div class="label">Home Phone:</div><div class="input"><input size="40" type="text" name="H_PHONE" value="<?php echo $row['H_PHONE']; ?>" /></div>
<div class="label">Cell:</div><div class="input"><input size="40" type="text" name="H_CELL" value="<?php echo $row['H_CELL']; ?>" /></div>
<div class="label">Birth Date:</div><div class="input"><input size="40" type="text" name="H_WEB" value="<?php echo $row['H_WEB']; ?>" /></div>
<div class="label">Membership Exp:</div><div class="input"><input size="40" type="text" size="40" name="MEMBER_INFO" value="<?php echo $row['MEMBER_INFO']; ?>" /></div>
<div class="label">Retired Mil Info:</div><div class="input"><input size="40" type="text" name="B_STATE" value="<?php echo $row['B_STATE']; ?>" /></div>
<div class="label">1st tour from:</div><div class="input"><input size="40" type="text" name="B_COUNTRY" value="<?php echo $row['B_COUNTRY']; ?>" /></div>
<div class="label">1st tour Rank/Rate:</div><div class="input"><input size="40" type="text" name="B_WEB" value="<?php echo $row['B_WEB']; ?>" /></div>
<div class="label">1st tour cruise Book:</div><div class="input"><input size="40" type="text" name="B_PHONE" value="<?php echo $row['B_PHONE']; ?>" /></div>
<div class="label">2nd tour from:</div><div class="input"><input size="40" type="text" name="B_FAX" value="<?php echo $row['B_FAX']; ?>" /></div>
<div class="label">2nd tour Rank/Rate:</div><div class="input"><input size="40" type="text" name="PAGER" value="<?php echo $row['PAGER']; ?>" /></div>
<div class="label">2nd tour cruise Book:</div><div class="input"><input size="40" type="text" name="B_COMPANY" value="<?php echo $row['B_COMPANY']; ?>" /></div>
<div class="label">3rd tour from:</div><div class="input"><input size="40" type="text" name="B_TITLE" value="<?php echo $row['B_TITLE']; ?>" /></div>
<div class="label">3rd tour Rank/Rate:</div><div class="input"><input size="40" type="text" name="B_DEPT" value="<?php echo $row['B_DEPT']; ?>" /></div>
<div class="label">3rd tour cruise book:</div><div class="input"><input size="40" type="text" name="OFFICE_LOC" value="<?php echo $row['OFFICE_LOC']; ?>" /></div>
</div>
<div id="rightcase">
<div class="label">Biography:</div><div class="input"><textarea rows="35" cols="80" name="NOTES" /><?php echo $row['NOTES']; ?></textarea></div>
<input type="hidden" id="record" name="record" value="<?php echo $_POST['record']; ?>"/>
<input type="hidden" id="formSubmit" name="formSubmit" value="<?php echo $_POST['formSubmit']; ?>" />
<input type="hidden" id="formApprove" name="formApprove" value="<?php echo $_POST['formApprove']; ?>" />
<input type="hidden" id="formDelete" name="formDelete" value="<?php echo $_POST['formDelete']; ?>" />
<input type="hidden" id="numOfRows" name="numOfRows" value="<?php echo $num_rows; ?>" />
<input type="hidden" id="ID" name="ID" value="<?php echo $_POST['ID']; ?>"/>
</div>
<div id="buttons">
<input type="submit" name="update" value="Update Record" onclick="formChange(this.form.elements['formSubmit'].value)" > - <input type="submit" name="decrease" value="Previous" onclick="nextPrevious(this.form.elements['record'], this)" > - <input type="submit" name="increase" value="Next" onclick="nextPrevious(this.form.elements['record'], this)" > - <input type="submit" name="delete" value="Delete Record" onclick="deleteRecord(this.form.elements['formSubmit'].value)" >
</form>
</body>
You probably have a name clash with the function and the name of the element.
Either rename the function or rename the textbox.
I have two forms on a single page and I want to submit the forms individually using the corresponding links from a query mobile navbar. The forms submit fine but the content portion of the page goes blank (I believe the jquery mobile ajax is trying to transition to a page that is not specified). Is there a way to stop it trying to transition and just submit the form data?
HTML:
<div class="ui-grid-a">
<div class="ui-block-a">
<form name="adminform" id="adminform">
<legend>Administrator</legend>
<input type="text" name="user_id" id="user_id" value="<?php echo $user_data['user_id'];?>" />
<label for="first_name">First name*:</label>
<input type="text" name="first_name" id="first_name" value="<?php echo $user_data['first_name'];?>" placeholder="First name" />
<label for="last_name">Last Name*:</label>
<input type="text" name="last_name" id="last_name" value="<?php echo $user_data['last_name'];?>" placeholder="Last Name" />
<label for="email">E-Mail*:</label>
<input type="email" name="email" id="email" value="<?php echo $user_data['email'];?>" placeholder="E-Mail" />
</form>
</div><!--end block a-->
<div class="ui-block-b">
<form name="companyform" id="companyform" action="" method="post">
<legend>Billing Address</legend>
<!--<label for="companyid">Company Id:</label>-->
<input type="hidden" name="companyid" id="companyid" value="<?php echo $company_data['company_id'];?>" />
<label for="company_name">Company Name*:</label>
<input type="text" name="company_name" id="company_name" value="<?php echo $company_data['company_name'];?>" />
<label for="company_website">Website:</label>
<input type="text" name="company_website" id="company_website" value="<?php echo $company_data['company_website'];?>" />
<label for="company_address">Address*:</label>
<input type="text" name="company_address" id="company_address" value="<?php echo $company_data['company_address'];?>" />
<label for="company_suite">Suite:</label>
<input type="text" name="company_suite" id="company_suite" value="<?php echo $company_data['company_suite'];?>" />
<label for="company_city">City*:</label>
<input type="text" name="company_city" id="company_city" value="<?php echo $company_data['company_city'];?>" />
<label for="company_state">State*:</label>
<input type="text" name="company_state" id="company_state" value="<?php echo $company_data['company_state'];?>" />
<label for="company_postal_code">Postal Code*:</label>
<input type="text" name="company_postal_code" id="company_postal_code" value="<?php echo $company_data['company_postal_code'];?>" />
<label for="company_telephone">Telephone*:</label>
<input type="text" name="company_telephone" id="company_telephone" value="<?php echo $company_data['company_telephone'];?>" />
<label for="company_fax">Fax:</label>
<input type="text" name="company_fax" id="company_fax" value="<?php echo $company_data['company_fax'];?>" />
</form>
<div id="companyupdatestatus"></div>
</div><!--end block b-->
</div><!--end grid a-->
<div data-role="navbar">
<ul>
<li><a id="updateadmin" data-icon="check" data-ajax="false">Update Admin</a></li>
<li><a id="updatecompany" data-icon="check" data-ajax="false">Update Company Info</a></li>
<li>Change Password</li>
</ul>
</div>
</div>
Javascript:
$('#updatecompany').click(function() {
var company = $('#companyform').serializeArray();
/*$('#companyupdatestatus').html(company);*/
$.ajax({
type: "POST",
url: 'companyupdate.php',
data: company,
success: function(company) {
$('#companyupdatestatus').html(company);
}
});
});
Thanks in advanced, would really appreciate the help!!! Adam
Try return false like this:
$('#updatecompany').click(function() {
var company = $('#companyform').serializeArray();
/*$('#companyupdatestatus').html(company);*/
$.ajax({
type: "POST",
url: 'companyupdate.php',
data: company,
success: function(company) {
$('#companyupdatestatus').html(company);
}
});
return false;
});
I have this form for a dedicated iphone landing page. but cant find anything that will deal with the validation. i only need to validate the number but the name could also be usefull. i cant seem to find anything that will alert the user the field has not been entered.
this is iphone only and is a web form not a app
<form method="post" action="submitquote.php" name="quoteform">
<label>Your Full Name</label>
<input style="outline:none;" id="name" type="text" name="name" size="20" value="" class="elements"/>
<label>Your Email Address</label>
<input style="outline:none;" id="email" type="email" name="email" size="20" value="" class="elements"/>
<label>Your Telephone Number</label>
<input style="outline:none;" id="telephone" type="text" pattern="[0-9]*" name="telephone" size="20" value="" class="elements">
<label>Company Postcode</label>
<input style="outline:none;" id="postcode" type="text" name="postcode" size="20" value="" class="input" />
<div class="saveon">Save on..</div>
<div class="checkboxs">Gas<input type="hidden" name="gasresult" value="no" /><input name="gasresult" type="checkbox" value="yes" checked="checked" /> Electricity<input type="hidden" name="electricresult" value="no" /><input name="electricresult" type="checkbox" value="yes" checked="checked" /></div>
<input type="hidden" value="<?php echo $myid ?>" name="track" />
<input type="hidden" value="<?php echo $_GET['utm_source']; ?>" name="utm_source" />
<input type="hidden" value="<?php echo $_GET['utm_medium']; ?>" name="utm_medium" />
<input type="hidden" value="<?php echo $_GET['utm_term']; ?>" name="utm_term" />
<input type="hidden" value="<?php echo $_GET['utm_content']; ?>" name="utm_content" />
<input type="hidden" value="<?php echo $_GET['utm_campaign']; ?>" name="utm_campaign" />
<input type="submit" value="" class="submit" onclick="pageTracker._trackPageview('/HomePage/ClassicQuoteRequest/Submit')" />
</form>
You can use JavaScript to get the value of an input object of a form.
function nameOK() {
var myForm = document.quoteform; // document.#nameOfForm#
var input = myForm.name;
var content = input.value;
return (content != "");
}
It is untested, but I have recently done something similar to store cookies.
Hope it works,
ief2