amy lee
amy lee

Reputation: 43

how to insert php statement into jquery/javascript

I am new in php. When user select one of the option in dropdown menu, the list of selected item will shows in textarea form. I want to make each of every item on that list, link to another page. How do I write php/html statement in javascrip/jquery? Here is my javascript :

<script type="text/javascript">
function check(){
var select = document.getElementById('category');
var textarea = document.getElementById('model');


 $.ajax({
            type: 'POST',
            url: 'http://localhost/system/ajax.php',
            data: {txt:$('#category').val()},
            dataType: 'json',
            success: function(resp){  

            result = resp.success;

            textarea.value = "";
            for(i=0; i<result.length; i++){
            textarea.value = result[i] += "\n" + textarea.value;
            }
        }
    });

}

This is my html code :

<form action="" method="post">

<tr>
<td width="116">Category</td>
<td width="221">
    <center>
        :
        <select name="category" id="category" onChange="check()">
            <option>--- Choose Category ---</option>
            <?php
                mysql_connect("localhost", "root", "");
                mysql_select_db("inventory");
                $sql = mysql_query("SELECT * FROM equipment GROUP BY equip_category ASC ");
                if(mysql_num_rows($sql) != 0){
                    while($row = mysql_fetch_array($sql)){
                        echo '<option value="'.$row['equip_category'].'">'.$row['equip_category'].'</option>';
                    }
                }
            ?>
        </select >
    </center>
</td></td></tr>
<tr>
<td><p>Model/Brand</p>
<td>
    <p align="center">:<textarea name="model" id="model"  rows="5" cols="25"><?php echo (''); ?></textarea>
    </p>
</td></td>
</tr></form>

Upvotes: 2

Views: 219

Answers (3)

amy lee
amy lee

Reputation: 43

this is my ajax.php :-

  <?php

if(!empty($_POST)) {

include "connect.php";
$txt = $_POST['txt'];
$query = "SELECT equip_name_desc FROM equipment WHERE equip_category = '$txt'";
$number = 0;
$result = mysql_query($query)


or die(mysql_error());

while($row = mysql_fetch_array($result))
{

$time = $row['equip_name_desc'];
$testarray[$number] = $time;
$number = $number+1;

}                
echo json_encode(array('success'=>$testarray));

   } 

?>

Below is the example of output. I want to make 'Lenovo GGG' or 'HP' is a link to another page. Question is, how to write the href link in javascript?

enter image description here

Upvotes: 0

Skurhse Rage
Skurhse Rage

Reputation: 1020

To insert PHP into jQuery/JS, use the following syntax:

var phpVar = <?php echo $some_php_var;?>;

console.log(phpVar); //logs $some_php_var

However, oftentimes it is best to just echo jQuery/JS in PHP:

<?php

echo 'var phpVar = '.$some_php_var.';',

'console.log(phpVar);';

?> <!--logs $some_php_var-->

Upvotes: 0

Rahul Desai
Rahul Desai

Reputation: 15501

In PHP use $_POST['txt'] to access the value of category that you are passing from Javascript.

Check out this tutorial for connecting PHP-Javascript-MySQL.

Upvotes: 0

Related Questions