1

I have a form like this: -

<form id="form" name="form" action="banner_ad_post.php" method="post" enctype="multipart/form-data">
                <p>
                  <label for="banner_name"><font color="#FF0000"> * </font>Banner Name: </label>
                  <input type="text" name="banner_name" id="banner_name" value="" maxlength="100" required="required"/>
                </p>
                <p>
                  <label for="Banner_website_url"><font color="#FF0000"> * </font>Banner website Url: </label>
                  <input type="url" name="banner_site_url" id="banner_site_url" value="" maxlength="100" required="required"/>
                </p>

                <p>
                  <label for="banner_image_url">Banner Image Url: </label>
                  <input type="file" name="file" id="file" value="" accept="image" placeholder="Browse from hard disk" onchange="img_path()"/> &nbsp;
                   <font color="#FF0000"> OR</font> &nbsp;
                  <input type="url" name="banner_image_url" id="banner_image_url" value="" maxlength="100" placeholder="Enter the url from website." onchange="validate()"/> &nbsp; 


                </p>
                <p>
                <label for="submit"> </label>
                  <input type="submit" id="submit" name="submit" value="Submit" />
                </p>
              </form>

And the JavaScript code like : -

<script>

    function validate()
    {
    var banner_name=document.getElementById("banner_name");
    var banner_site_url=document.getElementById("banner_site_url");
    var banner_file=document.getElementById("file");
    var banner_image_url=document.getElementById("banner_image_url");
    //var x=banner_file.value;
    //var y=banner_image_url.value;
    if((banner_file.value=true) && (banner_image_url.value=true))
        {
        alert("Please choose only one field either url or image uploading.");
        document.getElementById("form").reset();

        }
    }

    function img_path()
    {
    var file_path=document.getElementById('file').value;
    if(file_path.match(".jpeg$")==".jpeg" || file_path.match(".gif$")==".gif" || file_path.match(".GIF$")==".GIF" || file_path.match(".JPEG$")  ==".JPEG" || file_path.match(".JPG$")==".JPG" ||file_path.match(".jpg$")==".jpg" || file_path.match(".png$")==".png" || file_path.match(".PNG$")==".PNG")
    {
      return true;
    }
   else
   {
   alert("Upload  .jpg/.gif/png  file olny");
   document.getElementById("form").reset();
   return false;
   }

}
</script>

Now, the problem is that, whenever I am trying to enter the only image url in the form. It generates an alert message stating " (Please choose only one field either url or image uploading) ". But according to my definition the message should have to be displayed only when both the filed banner_image_url and file are set true.

Any help will be appreciated...
Thanx in advance.

2
  • There are probably more problems, but the first thing that pops up for me is that banner_file.value=true is an assignment, not a comparison. You need == instead. Commented Nov 24, 2012 at 19:19
  • You're using assignment (=) when you should use equality operator(==) : if((banner_file.value=true) && (banner_image_url.value=true)) Commented Nov 24, 2012 at 19:20

3 Answers 3

3

You need to use == instead of =

if((banner_file.value==true) && (banner_image_url.value==true))
Sign up to request clarification or add additional context in comments.

2 Comments

hey Ben! it is still not working. Now the problem is even more complex.The alert message is not popping at all even after providing both the fields.
+1, but you could also add an explanation of why ==true is unnecessary.
1

Doing this:

banner_file.value=true

sets the value of banner_file.value, and returns true, unconditionally.

This is not what you want, since you want to check the existing value of banner_file.value.

On the other hand, doing this:

banner_file.value==true

returns true only if the value of banner_file.value evaluates to true. This is what you want to do.

Try it with the following changes:

if((banner_file.value) && (banner_image_url.value))

Comments

0

This:

if((banner_file.value=true) && (banner_image_url.value=true))

should be:

if((banner_file.value == true) && (banner_image_url.value == true))

because, in your code you are setting the variable to true instead of checking if it is true. But if you want as little code as possible you could write:

if(banner_file.value && banner_image_url.value)

Explicitly testing for == true is redundant - but it can make the code more readable for some. Just thought I'd mention it in case you found the last example to be cleaner :)

3 Comments

Others beat me to it I see :)
Is it really boolean true, though?
@WolfgangStengel Nope, of course not. Sorry. Fixed it now. I've gotten a little too used to using === automatically I see :)

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.