I have the following codes, I wanted to show a message box when the value in month (in the textbox) is less than the value of $month (date("n")), when the next button is clicked. When I run my code, the next button disappears! Please help me to solve my problem. Thanks in advance!

<?php
$month = date("n");
?>

<br><font face = " century gothic" color = "#FFFC17" size = "2">Month:
<input name="addnum" type="text" placeholder= <?php echo $month ?> size = "1" id = "month">

<input type="submit" name="Submit" id="next" value=" Next " onClick="
<?php
if(document.getElementById('month') < $month){
alert('That month has already passed! Please put a valid month.');
}
?>
">

Recommended Answers

All 5 Replies

u cant write a javascript code in php tag. for do this , use from echo.

<?php
echo "if(document.getElementById('month').value < $month){";
echo "alert('That month has already passed! Please put a valid month.');}";
?>

getElementById('month') returns a object but you need its value.

commented: thank you so much! :) +0

Yehey! Thank you very much! That code worked perfectly! But now, I have another problem. I wanted to proceed to the next page if my value in month (a textbox) is equals or greater than the value of $month ($date('n')), but not greater than 12, because there are only 12 months. I have this code here but can't seem to make it work. Please help!!!

<input type="submit" name="Submit" id="next" value=" Next " onClick="
<?php
echo "if(document.getElementById('month').value < $month){";
echo "alert('That month has already passed! Please put a valid month.');}";
echo "if(document.getElementById('month').value => $month && document.getElementById('month').value <13){";
echo "window.location.href='cusdet.php';}}";
?>">

how do u want to send $month value to next page? u should use from the post or get method in your form.
write this javascript function into the head tag :

<script type="text/javascript">
function    submit_frm(){
    if(document.getElementById('month').value < $month){
        alert('That month has already passed! Please put a valid month.');
        return  false;
    }
    if(document.getElementById('month').value < 13){
        alert('Month value cant be more than 12');
        return  false;
    }
    return  true;
}
</script>

and write this for your form :

<form action="cusdet.php" onsubmit="return submit_frm()" method="...">
<font face = " century gothic" color = "#FFFC17" size = "2">Month:<input name="addnum" type="text" placeholder= <?php echo $month ?> size = "1" id = "month">
<input type="submit" name="Submit" id="next" value=" Next "/>
</font>
</form>

hello there thank you so much!

Whenever I put a date (M-D-YYYY) (The date today is 8-21-2012) equals to 8-21-2012 up to 8-31-2012, the alert box saying 'Invalid input keeps in' showing up but then forwards to the next page. I don't want the alert box to show up since the value to be entered is correct. Please help!!! Here's my code, thanks in advance!!!

<input type="text" placeholder= <?php echo $month ?> size = "1" id = "month">
<input type="text" placeholder= <?php echo $day ?> size = "1" id = "day">
<input type="text" placeholder= <?php echo $year ?> size = "1" id = "year">

<input type="submit" name="Submit" id="next" value=" Next " onClick="
<?php
echo "if (document.getElementById('month').value == $month && document.getElementById('day').value >= $day && document.getElementById('day').value < 32 && document.getElementById('year').value == $year)";
echo "window.location.href='paytrans.php';";

echo "if (document.getElementById('month').value > $month && document.getElementById('month').value < 13 && document.getElementById('day').value >= 1 && document.getElementById('day').value < 32 && document.getElementById('year').value == $year)";
echo "window.location.href='paytrans.php';";

echo "else{";
echo "alert('Sorry. Invalid input. Please input a valid date. (M-D-YYYY)');}";  
?>">

try this code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>
<?php
    $month = date("n");
    $day   = date("j");
    $year  = date("Y"); 
?>
<script type="text/javascript">
function    submit_frm(){
    var m = document.getElementById('month').value;
    var y = document.getElementById('year').value;    
    var d = document.getElementById('day').value;
    if (m > 12 || m < 1 || m < <?php echo $month; ?>){
        alert("Invalid month.");
        return  false;
    }
    if (d > 31 || d < 1 || d < <?php echo $day; ?>){
        alert("Invalid day.");
        return  false;
    }
    if (y != <?php echo $year; ?>){
        alert("Invalid year.");
        return  false;
    }
    return  true;
}
</script>
</head>

<body>
<form action="paytrans.php" onsubmit="return submit_frm()" method="post">
<font face = " century gothic" color = "#FFFC17" size = "2">
<input type="text" placeholder= "<?php echo $month ?>" size = "1" id = "month" name="month"/>
<input type="text" placeholder= "<?php echo $day ?>" size = "1" id = "day" name="day"/>
<input type="text" placeholder= "<?php echo $year ?>" size = "1" id = "year" name="year"/>
<input type="submit" name="Submit" id="next" value=" Next "/></font>
</form>
</body>
</html>

then u can get values of month & day & year on the paytrans.php file , ie:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php
    echo $_POST["month"]."/".$_POST["day"]."/".$_POST["year"];
?>
</body>

</html>

Sorry for my previous post , i have a tiny mistake there @ line 4 : <?php echo $month; ?> must be instead $month.
if u had any problem , say me.

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.