0

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.

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by jmichae3
0

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.

0

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>

Edited by jmichae3: n/a

0

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;

0

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']; ?>">
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.