Click to See Complete Forum and Search --> : [RESOLVED] JS not run if textbox data retrieved from mysql


techissue2008
07-17-2008, 01:22 AM
Hi

If the following code used with a pure html or php form, it does work. However, when I put it to edit price page, the text box retrieved data from DB and display in text box, it does not run. I tried both onclick and onsubmit() but both were failed to call it in edit price page.


name="priceform"
onclick="return checkprice();"
function checkprice()
{
var fi = document.priceform;
if(fi.price.value=="")
{
window.alert('Please input price');
fi.price.focus();
return false;
}
}


How should I fix it?

Fang
07-17-2008, 01:35 AM
Please give the full code; html & JavasScript

techissue2008
07-17-2008, 01:41 AM
HTML
<form method="post" action="<?php echo $PHP_SELF; ?>" name="priceform">
<input name="price" type="text" value="<?php echo $price;?>">
<input name="update" type="submit" value="Edit" onclick="return checkprice();">

JS
function checkprice()
{
var fi = document.priceform;
if(fi.price.value=="")
{
window.alert('Please input price');
fi.price.focus();
return false;
}
}

PHP
if(isset($_POST['update']))
{
$price = $_POST['price'];
$query = "UPDATE product SET price = '$price' WHERE productid = '$productid'";
mysql_query($query) or die('Error : ' . mysql_error());
}


It does not call the JS and run the PHP Query code.

Fang
07-17-2008, 01:54 AM
Runs OK
What errors occur?

techissue2008
07-17-2008, 02:24 AM
If I input " " instead of a number 123, It does not call the JS and run the PHP Query code.

Originally, JS can validate the input must not empty.

and then I changed the js to be


if(fi.price.value==""||isNaN(fi.price.value))
{
window.alert('Please input price');
fi.price.focus();
return false;
}


to check whether it is a number. However, I can input "ABC" and then pass it to query. Since the data field is bigint, it returned an error for not matching data type.

Fang
07-17-2008, 02:36 AM
if(!/\d+/.test(fi.price.value))

techissue2008
07-17-2008, 02:40 AM
It is not the problem, the JS can run on other php pages which do not need to retrieve data. It can run on a form.

Fang
07-17-2008, 02:42 AM
It is not the problem, the JS can run on other php pages which do not need to retrieve data. It can run on a form. :confused:
Is this problem some how related to your post on date format?

techissue2008
07-17-2008, 07:35 AM
Of course not.

I found the solution.

return parent.checkprice();

and it can call it now.