if(isset())函数未按预期工作

时间:2018-04-16 01:10:50

标签: javascript php mysql

我一直在尝试通过HTML和PHP学习基本的表单数据捕获。这是我这么多天以来一直困扰的地方,并且已经搜索了不同的网站以寻求解决方案。到目前为止,没有任何改变。

我已经给出了我正在使用的代码,以下是我面临的问题:

a)如果(isset())函数正在运行其中的所有代码,甚至在我们点击提交之前

b)数据未插入表中 - 无错误

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>
如果我能理解我哪里出错了,那将会有很大的帮助。

提前多多感谢

0 个答案:

没有答案