1

I am new to Netbeans. I have created a simple php project. It consists of a form and I've written a separate Javascript file to validate its fields. I tried running the project outside netbeans, it works fine. I recently imported the project to netbeans but javascript doesn't seem to work well. When I try opening the page(form page) in the browser, the entire javascript file appears on the page. I do not know where I'm going wrong. As I'm new to netbeans I am unable to rectify it. Kindly help.

My Javascript file is named Validation.js and is in the js folder.

function ValidateForm(theForm)
{
  //returns true/false after validation 
}

My php file looks similar to this.

<?php include "js/Validation.js"; 
  //if validation returns true
 if(isset($_POST))
 {
    if(isset ($_POST['bt_save']) && $_POST['bt_save'] == 'Submit')
    {
         //code to insert fields into database after validation
    }
 }
?>
<form method="post" name="form1" id="form1" onsubmit="return ValidateForm(this);">
 <!--form elements-->
 <input type="submit" name="bt_save" value="Submit"/>
</form>

Thanks in advance!

1 Answer 1

1

Your include is wrong, if you use php include it just loads the page.

Try using <script type="text/javascript" src="js/Validation.js"></script>. instead of include "js/Validation.js".

So your new file would be something like.

<?php
  //if validation returns true
 if(isset($_POST))
 {
    if(isset ($_POST['bt_save']) && $_POST['bt_save'] == 'Submit')
    {
         //code to insert fields into database after validation
    }
 }
?>
<form method="post" name="form1" id="form1" onsubmit="return ValidateForm(this);">
 <!--form elements-->
 <input type="submit" name="bt_save" value="Submit"/>
</form>
<script type="text/javascript" src="js/Validation.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Or alternatively in the above used PHP fashion try this : <?php echo '<script>'; include "js/Validation.js"; echo '</script>';. Dirty but will work.. ;)
@Tim Thank you.. It works now.. And yes it seems that the page gets loaded and that is the reason it gets displayed. However outside Netbeans it seems to work fine. Thanks again.

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.