从输入文本域中获取数据,然后在同一页面上的MySQL语句中使用它


Getting data from a input textfield then use it in MySQL statement on the same page

对不起,代码太多了:)

我需要在输入文本字段名称clientname输入客户端名称,然后我需要从我的数据库输出客户端信息,当我点击一个按钮名称loadcliinfo。但问题是我不知道我将如何获得在clientname输入的数据?所以我使用 $ _POST['列出']。代码不工作

我粘贴在下面的代码位于同一个文件中。

       <tr>
          <td width="6"></td>
          <td width="155">Client Name<font color="#990000">*</font></td>
          <td width="218"><input type="text" name="clientname" id="clientname" required= "required" class="forinput" value="<?php echo $clientname ?>"/></td>
           <td width="148"><input type="button" name="loadcliinfo" value="Load Client Info" onClick="loadclientinfo()"/></td>
          </tr>
          <tr>
            <td width="6"></td>
            <td width="155">Client Address<font color="#990000">*</font></td>
            <td width="218"><p id="clientadd" class="readonly"></p></td>
            <td width="148">Contact Name<font color="#990000">*</font></td>
            <td width="230"><p id="clientcontactname" class="readonly"></p></td>
            <td width="8"></td>
           </tr>
           <tr class="shade">
            <td width="6"></td>
            <td width="155">Email Address<font color="#990000">*</font></td>
            <td width="218"><p id="clientemail" class="readonly"></p></td>
            <td width="148">Contact No<font color="#990000">*</font></td>
            <td width="230"><p id="clientaddcontact" class="readonly"></p></td>
            <td width="8"></td>
          </tr>

我的php代码

      <?php
                $name=isset($_POST['clientname'])? $_POST['clientname'] : '';

我得到的数据使用这个语句isset($_POST['clientname'])?$_POST['clientname']: " 并将其存储在变量$name中,但当我输出变量时它为空。

                $csql="Select address, email, contactperson, contactno from client where name='$name'";
                $result=$db->prepare($csql);
                $result->execute();
                while($resu= $result->fetch(PDO::FETCH_ASSOC))
                    {
                       echo '<input type="hidden" id="add" value="'.$resu['address'].'"/>';
                        echo '<input type="hidden" id="email" value="'.$resu['email'].'"/>';
                        echo '<input type="hidden" id="contactperson" value="'.$resu['contactperson'].'"/>';
                        echo '<input type="hidden" id="contactno" value="'.$resu['contactno'].'"/>';    
                      }
                ?>

My loadclientinfo function

 <script>
  function loadclientinfo()
    { 
        var add=document.getElementById("add");
        var email=document.getElementById("email");
        var contactperson=document.getElementById("contactperson");
        var contactno=document.getElementById("contactno");
        document.getElementById("clientadd").innerHTML = add.value;
        document.getElementById("clientemail").innerHTML = email.value;
        document.getElementById("clientcontactname").innerHTML = contactperson.value;
        document.getElementById("clientcontact").innerHTML = contactno.value;
    }
</script>

我认为从clientname获取数据是问题。是否有任何方法可以从同一页面上的clientname获得数据?D

$_POST和$_GET从html表单获取它们的信息(大多数情况下)。您有输入字段,但没有表单。您需要有以下内容:

<form method="post" action="myphpscript.php">
  <input type="text" .... >
  <input type="submit" .... >
</form>

这个方法属性很重要,因为它决定了什么可以放到$_POST中,什么可以放到$_GET中。然后,您的操作将是它进入的php脚本。最后,在点击提交按钮之前,这些变量不会存储到本地POST会话中(假设您的方法是"POST")。现在,如果您想在不刷新页面的情况下完成整个操作,那么您必须做一些AJAX工作,以便在点击"提交"按钮时调用PHP脚本。

但这基本上就是问题所在。没有东西被放入本地存储。只是搞个杂耍。输入字段不做任何事情。如果您不想使用表单,那么您必须弄清楚如何以其他方式获取数据,而不需要使用$_POST。你可以构造一个GET url,然后使用$_GET,但通常不建议这样做,因为用户可以清楚地看到地址栏中的变量值,如:(你的url)?clientname=foobar.

关于PHP POST和GET的更多信息