Solved

Problem with html form processing

Posted on 2011-09-29
10
338 Views
Last Modified: 2012-08-13
I am trying to process a form without using "submit" button so I can do validation before submit, however the form seems to be submitted without me calling the submit function  after validation. It waits for validation to complete and then fires a submit. a few seconds later, depending on the browser. Here is the code I have extracted, I have double checked for any JQuery events and it seems not. I have extracted what I think is the relevant code but I can provide more.

<form name="cwccars" id='cwccars' action="php/createrecord.php" method="post">
<div id="labels">
<label for="registration">Registration</label>
<label for="carmake">Make</label>
<label for="carmodel">Model</label>
<label for="year">Year</label>
<label for="cost">Cost</label>
<label for="expenses">Expenses</label>
<label for="sell">Sold</label>
<label for="selldate">Date Sold</label>
<label for="seller">Salesman</label>

</div>
<div id="entries">
<input name="registration" id='registration' type="text" placeholder=" Registration Number" />
<input name="carmake" id='carmake' type="text" placeholder="/Pick Make of Car" />
<input name="carmodel" id='carmodel' type="text" placeholder="/Pick Model" />
<input name="year" id='year' type="text" placeholder=" Year of Manufacture" />
<input name="cost" id='cost' type="text" placeholder=" Cost ($)" />
<input name="expenses" id='expenses' type="text" placeholder=" Expenses ($)" />
<input name="sell" id='sell' type="text" placeholder=" Selling Price ($)" />
<input name="selldate" id="datepicker" type="text" placeholder="/Pick Date Sold" />
<input name="seller" id='seller' type="text" placeholder="/Pick Salesman" />
<button type="input" class='ajaxbutton' onClick="parent.process();">Enter Details</button>
<button type="input" class='ajaxbutton' onClick="parent.home();">Close</button>

</div>
<div id='morelabels'>
<label for="workdone">Expenses</label>
<textarea name="workdone" id="workdone" placeholder=" details of expenses"></textarea>
</div>
<div id='moreentries'>
<label for="comments">Comments</label>
<textarea name="comments" id="comments" placeholder=" any comments"></textarea>
</div>
</form>


function process()
{
carreg =  window.frames[0].document.getElementById('registration').value;
if (carreg=="") {
alert('Registration is a required field');
return;
}
yearstr=window.frames[0].document.getElementById('year').value;
yearint=parseInt(yearstr);
var d = new Date();
var this_year= d.getFullYear();
if(yearint>this_year)
{
alert('stuffed');
return;
}
alert('ok so far');
}

0
Comment
Question by:cookers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 14

Expert Comment

by:Kalpan
ID: 36813556
seems like it is not using the javascript function having the

<button type="input" class='ajaxbutton' onClick="parent.process();">Enter Details</button>

Please verify with elements error with mozilla firebug or IE inspect element tool
0
 

Author Comment

by:cookers
ID: 36813583
kalmax, I checked using bad data and the alerts are fired so it is calling that function
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36813640
You mean if validation fails, still form submits? If yes then Try like this

function
function process()
{
  carreg =  window.frames[0].document.getElementById('registration').value;
  if (carreg=="") {
  alert('Registration is a required field');
  return false;
  }
  yearstr=window.frames[0].document.getElementById('year').value;
  yearint=parseInt(yearstr);
  var d = new Date();
  var this_year= d.getFullYear();
  if(yearint>this_year)
  {
  alert('stuffed');
  return false;
  }
  alert('ok so far');
  return true;
}

Open in new window


html
<button type="input" class='ajaxbutton' onClick="return parent.process();">Enter Details</button>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Kalpan
ID: 36813683
does it displays alert('ok so far'); if everything works with process function ?
0
 

Author Comment

by:cookers
ID: 36813823
kalmax, yes it does display 'OK so far',
sonawanekiran: I did this
I did this

function process()
{
carreg =  window.frames[0].document.getElementById('registration').value;
if (carreg=="") {
alert('Registration is a required field');
return false;
}
yearstr=window.frames[0].document.getElementById('year').value;
yearint=parseInt(yearstr);
var d = new Date();
var this_year= d.getFullYear();
if(yearint>this_year)
{
alert('stuffed');
return false;
}
alert('ok so far');
return true;
}

and this

<button type="input" class='ajaxbutton' onClick="return parent.process();">Enter Details</button>

and still it processes the form...bummer hey?

0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 500 total points
ID: 36813851
If you want submit form then return true  otherwise return false
If you reach alert('ok so far'); then above code will submit the form
If you reach either alert('stuffed'); OR  alert('Registration is a required field'); then form will not be going submit.
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36813855
OR show your link
0
 

Author Comment

by:cookers
ID: 36813862
If I take out the form action and leave
<form name="cwccars" id='cwccars'>
it still blanks the form values after a few seconds after validation
0
 

Author Comment

by:cookers
ID: 36813902
sonawanekiran: your answer is correct, it does what I want but for technical reasons can you explain why it submits the form at all as I dont have "submit" anywhere iin my scripts or button type?
0
 

Author Closing Comment

by:cookers
ID: 36813905
Still confused
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question