I am trying to use javascript to validate text boxes on my form. I am currently using this code:

<script type="text/javascript"> 
function validate_form (){
	var myTextField = document.getElementById('Motor_Temperature');
	if(myTextField.value == "")
		alert("Please fill in Motor Temperature")
 return false;}
 else {
 return true;
}
</script>
</head>

But i keep getting an error on this line: else {
??????

I want to say if motor_temp is empty then display message and go back to form, otherwise save record.

Recommended Answers

All 4 Replies

You are missing a { on the end of line 4. That means the if only contains the debugging alert and the function always returns false. The else is outside the function.

Member Avatar for jmichae3

I am trying to use javascript to validate text boxes on my form. I am currently using this code:

<script type="text/javascript"> 
function validate_form (){
	var myTextField = document.getElementById('Motor_Temperature');
	if(myTextField.value == "")
		alert("Please fill in Motor Temperature")
 return false;}
 else {
 return true;
}
</script>
</head>

java and javascript are 2 way different things.


But i keep getting an error on this line: else {
??????

I want to say if motor_temp is empty then display message and go back to form, otherwise save record.

java and javascript are 2 very different things.

the bad indentation is causing you at least 2 bracket errors.

here is the correction and some friendly advice.

<script type="text/javascript">
//this code should be in the body after the element to work
function validate_form() {
	var myTextField = document.getElementById('Motor_Temperature');
        //having constant on left hand side prevents assignment errors if you have one = 
	if ("" == myTextField.value)  { 
		alert("Please fill in Motor Temperature");
		return false;
	} else {
		return true;
	}
}
</script>

That code worked so i receive the error message, but if i enter something into Motor Temperature, it doesnt save the record and take me back to my previous page?

i get a notice on this line even though my id field has something in it: Notice: Undefined index: id in C:\wamp\www\new.php on line 132

$id = $_POST;

Member Avatar for jmichae3

by the way, a common mistake is to think that you can use id's with PHP. you can't, you use name when you are using $_POST[] and $_GET[].

to use the old value on an input tag,there are 2 ways:

<?php echo "<input type='text' name='Motor_Temperature' value='".$_POST['Motor_Temperature']."'>"; ?>
<input type="text" name="Motor_Temperature" value="<?php echo $_POST['Motor_Temperature']; ?>">
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.