Edited : I’ve changed the code as follows and getting an ‘NaN’ error! Can someone give me a clue as to what that is and a tip to solve? Thanks.
Can someone please tell me whats wrong with this code?
- Firstly, I’ve called a Javascript within PHP, and is it really possible?
- Is there an issue with the way I’ve called the second alert? (despite whether its possible or not to use js within php) that meant to display the text input and the option selected in a alert message.
- Do I need to put ‘end of the line’ (;) after the last curly bracket of the loop?
Here is the code:
<?php
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQL="SELECT sbstart, sbend FROM newchk";
$run=mysql_query($SQL,$con) or die ("SQL Error");
$nor=mysql_num_rows($run);
?>
<!DOCTYPE html>
<html>
<head><title></title>
<script type="text/javascript">
function ValidateData() {
var TextIn = document.getElementById('txtN');
if(TextIn.value == ""){
alert ("Text field is empty");
return false;
}else{
var TextIn = document.getElementById('options');
alert (<?php $i ?> + TextIn.value);
}
}
</script>
</head>
<body>
<form onsubmit="return ValidateData()" method="POST">
<select STYLE='width:90px'><?php
while ($rec = mysql_fetch_array($run))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
{
echo "<option id='options' value=''.$i.''>$i<br></option>";
}
}
?>
</select>
<input type="text" name="txtN">
<input type="submit" name="subN" value="Save">
</form>
</body>
</html>
You should pass the form details in
onsubmit, your select doesn’t have a name, you cannot name the options, you need to name the select box:and your function: