Solved

Problem with html form processing

Posted on 2011-09-29
10
339 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
Independent Software Vendors: 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

726 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