0

I have been trying to learn basic Form data capture through HTML and PHP. This is the place I have been stuck since so many days and have searched different sites for a solution. So far, nothing changed.

I have given the code I am using below and following are the problems I am facing:

a) If (isset()) function is running all the code inside it, even before we click submit

b) Data is not getting inserted into the table - No Error as such

PHP:

$link = mysqli_connect($servername, $dbusername, $dbpassword, $database);

        if(! $link) {

          echo "Connection failed".mysql_error();
        } 

            if (isset($_POST['submit'])){

              $Project_Service_Name = $_POST['Project_Service_Name'];
              $Project_Desc = $_POST['Project_Desc'];

              $sql = "INSERT INTO mrc_projectmaster (Project_Service_Name, Project_Desc) VALUES ('$Project_Service_Name','$Project_Desc')";

              $result = mysql_query($link, $sql);

              if ($result) {

              echo " Data Successfully inserted";


              } else {

              echo "Please Enter Variables".mysql_error();

              }} 

HTML:

<form name = "ProjCreate" onsubmit = "return validatemyform();" method = "post" action = "">

  <div class = "form-group row">


      <label for = "Projname" class = "col-sm-3 col-form-label"> Project/Service Name : </label> 

      <div class = "col-sm-9">

      <input type = "text" Name = "Project_Service_Name" Placeholder = "Name of the Service/Project" class = "form-control" id = "Projname" >

      </div>

  </div>

  <div class = "form-group row">

      <label for = "Projdesc" class = "col-sm-3 col-form-label"> Project/Service Description: </label> 

      <div class = "col-sm-9">

      <textarea Name = "Project_Desc" Placeholder = "Description" class = "form-control" id = "Projdesc"></textarea>

      </div>

  </div>

  <div class = "form-group row">

    <div class = "col-sm-3">

    </div>

    <div class = "col-sm-9">      

    <input type = "submit" class = "btn btn-danger" Value = "Submit" Name = "Submit"> 

    </div>

  </div>

  </div>

</form>

JAVA SCRIPT:

<script>

  function validatemyform() {

    var formfield1 = document.forms["ProjCreate"]["Project_Service_Name"].value;
    var formfield2 = document.forms["ProjCreate"]["Project_Desc"].value;

    if (formfield1 == "" || formfield2 == ""){

       alert ("Please Fill all the Fields");

      } else {

         return true; 
      }

      }

</script>

Would be of real great help if I could understand where i Was going wrong.

Thanks a lot in advance

0 Answers0