Mario Laul Mario Laul - 4 months ago 8
PHP Question

How do I get the value of the selected customer to echo <input> element?

Database info:

CREATE TABLE
IF NOT EXISTS tz_customer (
id INT (10) NOT NULL,
customervarchar (255) NOT NULL,
NAME VARCHAR (255) NOT NULL,
vs_1 VARCHAR (255) NOT NULL,
stamp VARCHAR (255) NOT NULL
)


And PHP Code

THIS IS index.php FILE

<select name="nimetus" id="nimetus" onchange="chg(this.value)">


<?php $q3 = mysql_query("SELECT * FROM tz_kliendid"); while ($f3 = mysql_fetch_array($q3)) { ?>


<option value="<?php echo $f3['id']; ?>"><?php echo $f3['klient']; ?></option><?php } ?></select>


<input type="text" id="target" value="">


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


<script type="text/javascript">function chg(str){
$.ajax({
url: "showdata.php",
type: "POST",
data: {
"val": str
},
success: function (response) {
$("#target").val(response);
},
error: function (xhr) {
alert("Some error found!!");
}
});
}</script>


AND showdata.php FILE

<?php if(isset($_GET['val'])){$nimetus=$_POST['nimetus'];
$q1=mysql_query("SELECT vs_1 FROM tz_kliendid WHERE id='".$nimetus."' ORDER BY id DESC LIMIT 1"); $f1=mysql_fetch_array($q1); echo $f1['vs_1']; } ?>







Each customer has a "tz customer" table "vs_1" column marked 0 or 1.
When I choose a customer, how can I reach the selected customer "vs_1" value from the table?

And

How do I get the value of the selected customer to echo
<input>
element?

Answer

Hi I don't completely understand your question as far as i can understand you need vs_1 value of every customer when you change that select.You need to change your option value as id as it is the primary.You need ajax to do this

<select name="nimetus" id="nimetus" onchange="chg(this.value)">
<?php
  while ($f3 = mysql_fetch_array($q3)) {
   ?>
 <option value="<?php echo $f3['id'];  ?>">
      <?php echo $f3['customer'];  ?>
  </option>
   <?php
}
?> 
</select>
<input type="text" id="target" value="">

In java script

function chg(str){
  $.ajax({
            url: "showdata.php",
            type: "POST", //send it through post method
            data: {
                "val": str
            },
            success: function (response) {
                //Do Something
                $("#target").val(response);
            },
            error: function (xhr) {
                //Do Something to handle error
                alert("Some error found!!");
            }
        });
}

In showdata.php

if(isset($_POST['val']))//your POST method
  {
    $nimetus=$_POST['val'];
    $q1=mysql_query("SELECT vs_1 FROM tz_kliendid WHERE id='".$nimetus."' ORDER BY id DESC LIMIT 1");
   $f1=mysql_fetch_array($q1); 
   echo $f1['vs_1'];
  }    
Comments