By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

POST and Javascript

P: 2
<form action="insert_contact.php" method="POST">
Firstname
<input type="text" maxlength=30 name="fname">
<input type="submit" value="insert">
</form>
</body>
</html>

----------------------------------------------------------------------------

for the above HTML script, is there a way I could pass the variable fname to a javascript before it passes to PHP. I want javascript to check if there is a data on that field when 'insert' button is clicked, if not then cursor will go automatically back to the box or a perhaps a message will appear. And if there is data, javascript will just exit and give the rest of the procedure to PHP. Is that possible? Please help
Jul 12 '06 #1
Share this Question
Share on Google+
4 Replies


sashi
Expert 100+
P: 1,754
Hi there,

well.. yeah sure.. let do client side validation here.. ok

write a js fucntion to validate the contents of the "fname" field.. invoke the function on the OnSubmit event.. good luck my fren.. :)

come up with the js code first.. we will help you to validate your code..
Jul 12 '06 #2

iam_clint
Expert 100+
P: 1,208
<script>
function validateform() {
tmpname = document.getElementById('fname');
if (tmpname=="") {
alert('You didn't fill out the first name field');
} else {
document.insertform.submit();
}
}
</script>
<form action="insert_contact.php" method="POST" name="insertform">
Firstname
<input type="text" maxlength=30 name="fname" id="fname">
<input type="button" value="insert" onclick="validateform();">
</form>

</body>
</html>

You can also look up javascript setting focus by id on google to set the text field as the focus.
Jul 14 '06 #3

P: 2
Hi,
first of all thank you for responding and thank you for your time, however, it did not work. I noticed that there is missing <html> tag so I put it on the very top part of the script and the <body> right below the </script> but still no luck. please help
Jul 16 '06 #4

iam_clint
Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <script>
  4. function validateform() {
  5. var tmpname = document.getElementById("fname").value;
  6.     if (tmpname=="") {
  7.         alert("You didn't fill out the first name field");
  8.     } else {
  9.         document.insertform.submit();
  10.     }
  11. }
  12. </script>
  13. <form action="insert_contact.php" method="POST" name="insertform">
  14. Firstname
  15. <input type="text" maxlength=30 name="fname" id="fname">
  16. <input type="button" value="insert" onclick="validateform();">
  17. </form>
  18. </body>
  19. </html>
  20.  
my bad janjan -- this works as you can see i had to change a couple things i wasn't thinking completely and didn't test it before i posted it for you, the get element by id needed to have .value on it and the single quotes needed changed to double quote " this should work for you
Jul 16 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.