Jump to content

Recommended Posts

i have an onclick that calls a js function.

 

Am I able to have something like

 

onclick="if (condition) {do something}

 

if so. my condition would be my other js function that validates a form. here is my validation function.(special thanks to the members of phpfreaks for my validation function)

function validate(form1)
    {
    var valid = true;

    if (!form1.item_name.value)
    {
        document.getElementById('item_name_error').innerHTML = '*';
        valid = false;
    }
    else
    {
        document.getElementById('item_name_error').innerHTML = '';
    }
    
if (!form1.description.value)
    {
        document.getElementById('description_error').innerHTML = '*';
        valid = false;
    }
    else
    {
        document.getElementById('description_error').innerHTML = '';
    }

if (!form1.in_return.value)
    {
        document.getElementById('in_return_error').innerHTML = '*';
        valid = false;
    }
    else
    {
        document.getElementById('in_return_error').innerHTML = '';
    }

if (form1.listmenu.value == '1')
    {
        document.getElementById('listmenu_error').innerHTML = '*';
        valid = false;
    }
else
    {
        document.getElementById('listmenu_error').innerHTML = '';
    }

if (!form1.image.value)
    {
        document.getElementById('image_error').innerHTML = '*';
        valid = false;
    }
else
    {
        document.getElementById('image_error').innerHTML = '';
    }
return valid;
    }

Basicaly , what I'm looking for is for my onclick to check that the validation is good, and the run the onclick

Link to comment
https://forums.phpfreaks.com/topic/185542-if-statement-question/
Share on other sites

i have an onclick that calls a js function.

 

Am I able to have something like

 

onclick="if (condition) {do something}

 

Nope, that kind of thing isn't possible.  Event handlers must be functions.  What's stopping you from doing

 

someElement.onclick = function() {
   if (condition) { /* do something */ }
   else { /* do other thing */ }
}

 

??

I'm a little lost. Are you saying that would go into the input tag like a onclick would. I'm still learning here.

 

Ah, sorry.  I don't tend to use inline JavaScript (i.e., JavaScript that's placed inside HTML elements).  I prefer an unobtrusive  (JavaScript that's separate from the HTML) style as it's easier to maintain, debug, and alows sites to degrade gracefully if the user is viewing it with an incompatible browser.

 

So, my example would apply to a page that looks like:

 

<!DOCTYPE html>
<html>
   <head>
      <title>Unobtrusive JavaScript Example</title>
   </head>

   <body>
      <form name ="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
         <!-- Inputs go here -->
         <input type="submit" name="submit" value="submit" />
      </form>
   </body>

   <script type="text/javascript"/>
      var submit = document.forms["myForm"].elements["submit"];

      submit.onclick = function() {
         if (/* condition */) { /* Do something */ }
         else { /* Do other thing */ } 
      }
   </script>

</html>

Still a little confused. Sorry This is what I got so far. What I'm trying to do is if my validation passess when the submit button is clicked, then show the hidden div. If validation is not passed, then keep the div hiding.

<script type="text/javascript"/>
var submit = document.forms["form1"].elements["submit"];
submit.onclick = function()
{
if (valid = false)
{
  var messID = document.getElementById(message_box);
  messID.style.display = 'block';
}
else
{
  var messID = document.getElementById(message_box);
  messID.style.display = 'none';
}
}
</script>

Here is my div inside the form

<form action="postnewitem_test0167.php" method="post" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return validate(this);">
<table id="message_box" width="600" border="0" bgcolor="#9AA4AD" style="display:none">
                  <tr>
                  <td>
                  Uploading... Please be patient while your item is posting.</p>
                  </td>
                 </tr>
               </table>  
</form>

the return validate is my validation which is this.

<script type="text/javascript"/>
function validate(form1)
    {
    var valid = true;

    if (!form1.item_name.value)
    {
        document.getElementById('item_name_error').innerHTML = '*';
        valid = false;
    }
    else
    {
        document.getElementById('item_name_error').innerHTML = '';
    }
    
if (!form1.description.value)
    {
        document.getElementById('description_error').innerHTML = '*';
        valid = false;
    }
    else
    {
        document.getElementById('description_error').innerHTML = '';
    }

if (!form1.in_return.value)
    {
        document.getElementById('in_return_error').innerHTML = '*';
        valid = false;
    }
    else
    {
        document.getElementById('in_return_error').innerHTML = '';
    }

if (form1.listmenu.value == '1')
    {
        document.getElementById('listmenu_error').innerHTML = '*';
        valid = false;
    }
else
    {
        document.getElementById('listmenu_error').innerHTML = '';
    }

if (!form1.image.value)
    {
        document.getElementById('image_error').innerHTML = '*';
        valid = false;
    }
else
    {
        document.getElementById('image_error').innerHTML = '';
    }
return valid;
    }
</script>

 

I'm still attempting to learn javascript

You shouldn't have two functions here.  Just tie your div display function to your validate function:

 

function validate(form) {
   // all your previous validation code

   if (!valid) {
      var messID = document.getElementById("message_box");
      messID.style.display = "block";
   }

   return valid
}

 

You don't need an else-clause there as you should make the div have a display set to "none" by default in the CSS, and if the form is valid, the user will be sent to where ever the form's action takes them.

that works out pretty good. i'm really starting to like js. I just need to put some more time is to learning. The only thing i changed was

if (!valid)  to if(valid)

 

That way, if my form passes the validation, then it will display to div.

 

Thanks a bunch.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.