-2

hi i am new to php and javascript. i have a problem. i wanted to obtain a value from javascript function and pass it on to php and use that value for a query. here's the code:

<?php
$link=mysqli_connect("localhost","root","", "personinfo") or die("Error " .mysqli_error($link));
?>
<body>
<div id="qBox">
<p>Name: </p>
<select id="sel">
    <option value="1">Person1</option>
    <option value="2">Person2</option>
    <option value="3">Person3</option>
</select>
<button onclick="myFunction()">Submit</button>
</div>
<script>
    function myFunction(){
        var x = document.getElementById("sel").value;
    }
</script>
<?php
$did=x;
$sql="SELECT * FROM personinfo where id=$did limit 1" or die("Error in the consult.." . mysqli_error($link));
$aResult=mysqli_query($link, $sql);
while($rows = mysqli_fetch_array($aResult))
{ 
$id                 = $rows['ID'];
$pid              = $rows['personID'];
$fname         = $rows['firstName'];
$lname   = $rows['lastName'];
$department  = $rows['department'];
$type       = $rows['fType']; 
?>
<div id="combi">
<div id="tag">
    <p id="pColor">ID</p>
    <p id="pColor">FIRST NAME</p>
    <p id="pColor">LAST NAME</p>
    <p id="pColor">DEPARTMENT</p>
    <p id="pColor">TYPE</p>
</div>
<div id="content">
    <b><?php echo $pid;?><?php echo $id;?></b><br />
    <b><?php echo $fname;?></b><br />
    <b><?php echo $lname;?></b><br />
    <b><?php echo $department;?></b><br />
    <b><?php echo $type;?></b>
</div>
</div>
<?php }
?>
</body>

if there's a way to do that it will be much appreciated, if not, what do i need to do to make this work? i hope you could help. thanks.

  • 2
    have try using ajax or jquery? – Pragnesh Chauhan Nov 11 '14 at 08:30
  • You know php code is executed on server and JavaScript on client(=browser)? – Andreas L. Nov 11 '14 at 08:43
  • possible duplicate of [How to pass JavaScript variables to PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – anik4e Nov 11 '14 at 08:50
  • Why would you use js for this? You can use http methods (POST or GET). – Nergal Nov 11 '14 at 09:16
  • @PragneshChauhan - i might try using ajax or jquery as soon as i've finished up reading on them and as soon as i've found some examples. at the moment i am still at the phase of learning. – Kynaz dela Cruz Nov 12 '14 at 10:39
  • @NaincyGupta - i have tried javascript, i know its not an excuse that i do not know anything yet, i should spend more time reading. – Kynaz dela Cruz Nov 12 '14 at 10:40
  • @anik4e - i wasn't aware. i was browsing and haven't seen it on the suggested posts so i made a new one, anyway, if there has been a solution ill read on that. thanks for the link BTW. cheers. – Kynaz dela Cruz Nov 12 '14 at 10:41
  • @I3B13 - i am relatively new to this kind. thank you for the comment i will try using the POST and GET methods. anyway, someone posted an example around here so i might check those out. – Kynaz dela Cruz Nov 12 '14 at 10:42
  • @AndreasM. - actually, i'm a bit embarrassed because i already knew that php is on server and JavaScript on client. i've read about that but haven't seen examples yet. anyway thanks guys. – Kynaz dela Cruz Nov 12 '14 at 10:43
  • thank you guys, i hope to learn more. cheers. – Kynaz dela Cruz Nov 12 '14 at 10:44

1 Answers1

0

You need to submit a form using javascript for passing value in PHP. Try this-

CODE:

<html>
<?php
$link=mysqli_connect("localhost","root","", "personinfo") or die("Error " .mysqli_error($link));
?>
<body>
    <div id="qBox">
    <p>Name: </p>
    <select id="sel">
        <option value="1">Person1</option>
        <option value="2">Person2</option>
        <option value="3">Person3</option>
    </select>
    <button onclick="myFunction()">Submit</button>
    </div>
    <script>
        function myFunction(){
            var x = document.getElementById("sel").value;

            var form = document.createElement("form");
            input = document.createElement("input");

            form.action = "";
            form.method = "post"

            input.name = "x";
            input.value = x;
            form.appendChild(input);

            document.body.appendChild(form);
            form.submit();
        }
    </script>
    <?php
    $x = '';
    if(isset($_POST['x'])){ $x = $_POST['x'];}
    $did = $x;
    $sql="SELECT * FROM facultyinfo where id=$did limit 1" or die("Error in the consult.." . mysqli_error($link));
    $aResult=mysqli_query($link, $sql);
    while($rows = mysqli_fetch_array($aResult))
    { 
    $id                 = $rows['ID'];
    $pid              = $rows['personID'];
    $fname         = $rows['firstName'];
    $lname   = $rows['lastName'];
    $department  = $rows['department'];
    $type       = $rows['fType']; 
    ?>
    <div id="combi">
    <div id="tag">
        <p id="pColor">ID</p>
        <p id="pColor">FIRST NAME</p>
        <p id="pColor">LAST NAME</p>
        <p id="pColor">DEPARTMENT</p>
        <p id="pColor">TYPE</p>
    </div>
    <div id="content">
        <b><?php echo $pid;?><?php echo $id;?></b><br />
        <b><?php echo $fname;?></b><br />
        <b><?php echo $lname;?></b><br />
        <b><?php echo $department;?></b><br />
        <b><?php echo $type;?></b>
    </div>
    </div>
    <?php } ?>
</body>

anik4e
  • 493
  • 8
  • 16