0

sorry for the bulk of code :)

I need to input a client name in the input textfield name clientname then i need to output the client information from my database when i click a button name loadcliinfo. But the problem is i don't know how will i get the data inputted in clientname? so I used $_POST['clientname']. The code is not working.

the code i pasted below is located on the same file.

       <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>

my php code

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

I get the data using this statement isset($_POST['clientname'])? $_POST['clientname'] : '' and store it in the variable $name but when i output the variable its empty.

                $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>

I think getting data from clientname is the problem. Is there any way i could get the data from clientname on the same page? thanks :D

2
  • try using var_dump($_POST); to see what you are getting. Commented Jul 24, 2014 at 8:05
  • i tried this $name=var_dump(isset($_POST['clientname'])); but it output bool(false) Commented Jul 24, 2014 at 8:21

1 Answer 1

0

$_POST and $_GET get their information from an html form (for the most part). You have the input fields, but no form. You need to have something along the following:

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

That method attribute is important because that determines what gets put into $_POST and what gets put into $_GET. Then your action will be the php script that it goes to. And lastly, those variables don't get stored into the local POST session (assuming your method is "post") until the submit button is hit. Now if you want to do this whole thing without refreshing the page, then you'll have to do a bit of AJAX work to call the PHP script when the "submit" button is hit.

But that's basically the problem. Nothing is getting put into local storage. Simply having a misc. input field doesn't do anything. If you don't want to use a form, then you'll have to figure out how to get the data some other way, without $_POST. You cooouulldd contruct a GET url then use $_GET but that's not typically recommended as the user can plainly see the variable values in the address bar like: (your url)?clientname=foobar.

More on PHP with POST and GET

Sign up to request clarification or add additional context in comments.

1 Comment

So i cannot use $_POST and $_GET locally. Its cleared now. Thanks for the help :D

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.