0

The following echo statement creates an empty textbox and a submit button using php.

    echo"<td><form id=\"edit\" name=\"edit\" method=\"post\" action=\"../assets/modules/coursemanager/process.cm.php\" onsubmit=\"return check_edit();\">
    <input type=\"text\" size=\"3\" id=\"edit_places\" name=\"edit_places\" value=\"\" />
    <input type=\"hidden\" name=\"sid\" value=\"".$sid."\" />
    <input type=\"submit\" name=\"Submit\" value=\"edit places\" /></form></td>";

This is the validation JavaScript I am using, but the above form is ignoring it.

  function check_edit(){
      var notempty=document.forms["edit"]["edit_places"].value;
        if(notempty==null || notempty==""){
           alert("Please enter the new number of available places and try again");
           return false;
      }else{    
           return true;
      }
  }

I would be very grateful if someone could show me my error.

3
  • Not sure if this is your problem but notempty==nul || notempty=="" should be notempty==null || notempty=="". Also because of how Javascript works, a simpler way of saying that is if(!notempty), which will handle null and "" but also undefined false and 0. If you only want to check null/"", you should use === instead of just ==. Commented Jan 11, 2014 at 3:45
  • null is misspelled on the third line Commented Jan 11, 2014 at 3:46
  • Ah yes, thank you for that Josh. I have corrected that in the post :) Commented Jan 11, 2014 at 4:14

2 Answers 2

1

You can check the length of the input.

value = document.forms["edit"]["edit_places"].value.replace(/\s+/g, '')

if (value.length > 0) {
  return true;
} else {
  alert("Please enter the new number of available places and try again");
  return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer Knouroozi. I have resolved it as follows:
0

I used an else statement in the processing script which does the trick and dispensed with the check_edit function.

    if($_POST['edit_places']!== ''){
    $id=$_POST['sid'];
    $z=$_POST['edit_places'];
    $sql = "UPDATE xtra_session SET places=$z WHERE xtra_session.id=$id";
    $add = $modx->db->query($sql);
    $msg = ($add? "available places has been updated" : "error: ".mysql_error());
    }
    else {
    echo "Please enter a number of places to change"; 
    }

Thank you for your valued suggestions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.