i have a auto suggest textbox witch generates li tags with a onclick event with a function but i have to pass a php variable to the function as parameter but i only want the variable value of the li the user clicked on and now i always get the last variable in the loop.
And i want the var test to be the value of $test = $row['t_product']; of the li the user clicks on
here is the code im using now:
if(isset($_POST['search_term']) == true && empty($_POST['search_term']) == false){
$search_term = $_POST['search_term'];
$customerid = $_SESSION['userdata']['t_customer_id'];
$conn = connect();
$sql = "SELECT * FROM Licenses WHERE customer_id = '$customerid' AND t_name LIKE '$search_term%'";
$query = sqlsrv_query($conn, $sql);
while(($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) != false){
$test = $row['t_product'];
?>
<script type="text/javascript">
var test = <?php echo json_encode($test); ?>;
</script>
<?php
echo '<li onclick="show(test)">', $row['t_name'], '</li>';
}
}
i hope i explained my problem well and hope that some wane can help me :)
Well, maybe you should do :
while(($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) != false){
$test = $row['t_product'];
?>
<?php
echo '<li onclick="show("'.$test.'")">', $row['t_name'], '</li>';
}
or
if(isset($_POST['search_term']) == true && empty($_POST['search_term']) == false){
$index = 0;
$search_term = $_POST['search_term'];
$customerid = $_SESSION['userdata']['t_customer_id'];
$conn = connect();
$sql = "SELECT * FROM Licenses WHERE customer_id = '$customerid' AND t_name LIKE '$search_term%'";
$query = sqlsrv_query($conn, $sql);
while(($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) != false){
$test = $row['t_product'];
?>
<script type="text/javascript">
var test<?php echo $index++ ?> = "<?php echo $test; ?>";
</script>
<?php
echo '<li onclick="show(test'.$index++.')">', $row['t_name'], '</li>';
}
}
Edit: removed json_encode and replaced it with quotes
solved it by passing the $test value as id to each li and in the js get it with
$test = $row['t_product'];
echo '<li onclick="show(this)" id="'.$test.'">', $row['t_name'], '</li>'
function show(id){
alert($(id).attr("id"));}
Related
It is a tracking system like DHL. Tracking shipment number from MySQL database using php form.
but I need it Search multiple row separate by comma from mysql using php.
<?php
$ship=$_POST['Consignment'];
$cons = explode(',',$ship);
?>
<?php
$sql = "SELECT * FROM tbl_courier WHERE cons_no = '$cons[]'";
$result = dbQuery($sql);
$no = dbNumRows($result);
if($no == 1){
while($data = dbFetchAssoc($result)) {
extract($data);
?>
Shipment Name: <?php echo $ship_name; ?>
Shipment Phone: <?php echo $phone; ?>
<?php }//while
}//if
else {
echo 'In else....';
?>
Consignment Number not found.Search Again.
<?php
}//else
?>
So I need my search will work with separating by comma(,).
Thanks for helping me.
You can use IN operator in that case.
<?php
$ship=$_POST['Consignment'];
?>
<?php
$sql = "SELECT * FROM tbl_courier WHERE cons_no IN(".$ship.")";
$result = dbQuery($sql);
$no = dbNumRows($result);
if($no == 1){
while($data = dbFetchAssoc($result)) {
extract($data);
?>
Hope it will help to you.
change your sql query you have written '$cons[]' in select query which is wrong . after explode you will get data as 1,2,3 so you just need to write variable in query not array and user IN Operator like this.
`$sql = "SELECT * FROM tbl_courier WHERE cons_no IN(".$ship.")";`
You should always prepare/sanitize the POST data before using it in MySql query (in terms of security):
<?php
if (isset[$_POST['Consignment']] && !empty($_POST['Consignment'])) {
$ship = $_POST['Consignment'];
$cons = explode(',', $ship);
$cons = array_filter($cons, function($v){
return trim(strip_tags($v));
});
$cons = '"' . implode('","', $cons) . '"';
$sql = "SELECT * FROM tbl_courier WHERE cons_no IN ($cons)";
$result = dbQuery($sql);
$no = dbNumRows($result);
if ($no == 1) {
while ($data = dbFetchAssoc($result)) {
extract($data);
....
}
....
}
?>
Please Use Find IN SET
SELECT * FROM tbl_courier WHERE FIND_IN_SET(cons_no,'1,2,3,4,5')
Updated
SELECT * FROM tbl_courier WHERE FIND_IN_SET(cons_no,'$ship')
Note :- $ship Comma Separated Value not an array
I found the Answer:
if(isset($_POST['Consignment'])){
$ship=$_POST['Consignment'];
$shipment= explode(',',$_POST['Consignment']);
$ship = implode("', '",$shipment) ;
$query = "SELECT * FROM `tbl_courier` WHERE `cons_no` IN('$ship')";
$results = $mysqli->query($query);
if($results){
print '<table border="1">';
while($row = $results->fetch_assoc()) {
print '<tr>';
print '<td>'.$row["cons_no"].'</td>';
print '<td>'.$row["customerName"].'</td>';
print '<td>'.$row["customerPhone"].'</td>';
print '</tr>';
}
print '</table>';
// Frees the memory associated with a result
$results->free();
}
else {
echo "Query Not Match";
}
$mysqli->close();
}
Thanks to Answer.
I am modifying a site built in PHP with content drawn from a SQL database. The original developer had the navigation created with the following code. It creates the navigation from the CMS pages - it works fine for top level pages but I am trying to modify the way subpages are presented.
What I want it to do is when a subpage is present, it first opens a DIV containing formatting, then populates the subpagemenu by using the 'while' statement below. Finally, when the subpagemenu is finished, it closes the DIV.
I can't seem to work out which condition 'opens' the subpage menu before it goes through the loop of filling out the subpage menu.
Any and all help appreciated - thanks!
<?
$pagesrc = $_SERVER['SCRIPT_NAME'];
$getPID_sql = "SELECT * FROM tblPages WHERE parentID = 0";
$getPID_result = mysql_query($getPID_sql);
if(!$getPID_result){print mysql_error()."<br />";}else{
while ($row = mysql_fetch_array($getPID_result, MYSQL_ASSOC)) {
$parentID = $row["pageID"];
$linkName = $row["pageTitle"];
$linkID = $row["pageID"];
print "<A href = '".$pagename."?id=".$linkID."'>".$linkName."</A> | ";
if($pageID){
$subpages_sql = "SELECT * FROM tblPages WHERE parentID = $parentID";
$subpages_results = mysql_query($subpages_sql);
if(!$subpages_results){print mysql_error();}else{
$rowcount = mysql_num_rows($subpages_results);
if($rowcount > 0){
while ($row2 = mysql_fetch_array($subpages_results, MYSQL_ASSOC)) {
$sublinkName = $row2["pageTitle"];
$sublinkID = $row2["pageID"];
$sublinkParentID = $row2["parentID"];
if($sublinkParentID == $pageID || $sublinkParentID == $PID){
print "<a href='".$pagename."?id=".$sublinkID."'>".$sublinkName."</a>";
}
}
}
}
}
}
}
?>
<?
$pagesrc = $_SERVER['SCRIPT_NAME'];
$getPID_sql = "SELECT * FROM tblPages WHERE parentID = 0";
$getPID_result = mysql_query($getPID_sql);
if(!$getPID_result){print mysql_error()."<br />";}else{
while ($row = mysql_fetch_array($getPID_result, MYSQL_ASSOC)) {
$parentID = $row["pageID"];
$linkName = $row["pageTitle"];
$linkID = $row["pageID"];
print "<A href = '".$pagename."?id=".$linkID."'>".$linkName."</A> | ";
if($pageID){
$subpages_sql = "SELECT * FROM tblPages WHERE parentID = $parentID";
$subpages_results = mysql_query($subpages_sql);
if(!$subpages_results){print mysql_error();}else{
$rowcount = mysql_num_rows($subpages_results);
if($rowcount > 0){
echo "<div class='submenu'>"; //you can either give class or id whatever you want
while ($row2 = mysql_fetch_array($subpages_results, MYSQL_ASSOC)) {
$sublinkName = $row2["pageTitle"];
$sublinkID = $row2["pageID"];
$sublinkParentID = $row2["parentID"];
if($sublinkParentID == $pageID || $sublinkParentID == $PID){
print "<a href='".$pagename."?id=".$sublinkID."'>".$sublinkName."</a>";}
}
echo "</div>";
}
}
}
}
}
?>
added the tag where it is requrired
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Referring to the title. I have an array which I coded like this:-
$query = "SELECT * FROM server";
$result = mysql_query($query);
$dServer = array();
while($row = mysql_fetch_assoc($result)) {
$dServer[] = $row['model'];
}
Now, how do I pass the $dServer array into a Javascript array?
For example, this array:
var a = new Array();
$query = "SELECT * FROM server";
$result = mysql_query($query);
$dServer = array();
while($row = mysql_fetch_assoc($result)){
$dServer[] = $row['model'];
}
?>
<script type="text/javascript">
var a = <?php echo json_encode($dServer); ?>;
</script>
Encode it as a json object.
<?
$arr = array('entry' => 'content');
?>
<script>
var data = <?=json_encode($arr);?>;
alert(data['entry']);
</script>
Try to get use of ajax request and json_encode.
Second variant
<?php
$query = "SELECT * FROM server";
$result = mysql_query($query);
$dServer = array();
while($row = mysql_fetch_assoc($result))
{
$dServer[] = $row['model'];
}
?>
var a = <?php echo json_encode($dServer);?>;
In addition to the ajax / json methods mentioned, you can directly print out the values:
<?php
$query = "SELECT * FROM server";
$result = mysql_query($query);
?>
<script type="text/javascript">
var a = new Array();
<?php
while($row = mysql_fetch_assoc($result)){
echo "a['model'] = " . $row['model'] . ";";
echo "a['nextField'] = " . $row['nextField'] . ";";
}
?>
</script>
I have a little question for you!
I make a selection from mysql my buttons for example:
$query = mysql_query("select * from navigation");
while ($row = mysql_fetch_assoc($query)) {
echo '$row['name']';
}
so I call all the navigation from mysql and I have a class in css which is called as .active, this class make a active button when I click it, but how can I make the first button active?
$query = mysql_query("select * from navigation");
while ($row = mysql_fetch_assoc($query)) {
if ($row['id'] == $_GET['id'])) {
echo '<a class="active" href="?id=$row['id']">$row['name']</a>';
} else {
echo '$row['name']';
}
}
Try this.
<?PHP
$query = mysql_query("select * from navigation");
$first = true;
while($row = mysql_fetch_assoc($query)) {
if ((!array_key_exists('id', $_GET) && $first) || $row['id'] == $_GET['id']) {
$extra = 'class="active"';
$first = false;
} else
$extra = '';
echo "<a $extra href=\"?id={$row['id']}\">{$row['name']}</a>";
}
?>
1) I want to save case 1 value to an array. Return this array and pass it to a function. The code become case 2, but no result come out, where is the problem?
2) In function display_urls, i want to echo both $url and $category. What should i do in IF condition or add another line of code?
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url."";
echo "".$category."";
}
}
echo "";
}
case 1: work fine
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$i=0;
echo "";
while( $row = oci_fetch_array($result) ){
$i++;
echo "";
echo "".$row['USERNAME']."";
echo "".$row['BM_URL']."";
echo "".$row['CATEGORY']."";
echo "";
}
echo "";
case 2:
$url_array = array();
while( $row2 = oci_fetch_array($result, OCI_BOTH)){
$i++;
$url_array[$count] = $row[0];
}
return $url_array;
I think you probably want something like this:
function display_urls($url_array)
{
echo "";
if (is_array($url_array) && count($url_array)>0)
{
foreach ($url_array as $url)
{
echo "".$url['BM_URL']."";
echo "".$url['CATEGORY']."";
}
}
echo "";
}
$result = oci_parse($conn, "select * from bookmark where username ='$username'");
if (!$result){ $err = oci_error(); exit; }
$r = oci_execute($result);
$url_array = array();
while( $row = oci_fetch_array($result, OCI_ASSOC)){
$url_array[] = $row;
}
display_urls($url_array);
This will store all the information on the URLs in $url_array with a lookup by column name.