PHP Ajax drop down box - php

<?php
require ("init.php");
?>
<head>
<script src="ajax.js"></script>
<script src="common.js"></script>
</head>
<body>
<?php
$query = "SELECT * FROM User1";
$result = mysqli_query($connection, $query);
echo '<form> ';
echo "Select a Users:";
echo '<select name="users" onchange="showUser(this.value)">';
while ($row=mysqli_fetch_assoc($result)){
echo $row=['Username'];
echo '<option value="'.$row=['Username'].'">'.$row=['Username'].'</option>';
}
echo '</select></form>';
?>
<div id="txtHint"><b>User info will be listed here.</b></div>
</body>
the problem i am having is that the drop down box only shows array multiple times and does not show usernames from my database however the connection to my database is working as when tryed debugging it got it to echo out just one username

IT should be like this:
while ($row=mysqli_fetch_assoc($result)){
//echo $row=['Username']; //Invalied here
echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';
}
No need that =sign.

Correct:
echo $row=['Username'];// This is unnecessary.
echo '<option value="'.$row=['Username'].'">'.$row=['Username'].'</option>';// what is = doing here?
To:
echo '<option value="'.$row['Username'].'">'.$row['Username'].'</option>';

Related

Get selected value text from dropdown with XML AND PHP

I have the next code:
<form method="POST">
<?php
echo "<select value>";
$xml = new SimpleXMLElement("http://news.google.com/news?ned=us&topic=h&output=rss",null,true);
foreach($xml->channel->item as $news)
{
echo "<option value='".$news->title."'>" . $news->title . "</option>";
}
echo "</select>";
?>
</form>
So i have a dropdown box with news titles, when i select a title i want to appear the description from the xml file and i dont know how to do it, if someone can help me, please
Please check this example and let me know if you want any more help.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".feeds").change(function(){
var val=$(this).val();
$(".common").hide();
$("."+val).show();
});
});
</script>
<style>
.common{display:none;}
</style>
<?php
$xml = new SimpleXMLElement("http://news.google.com/news?ned=us&topic=h&output=rss",null,true);
$select_html="<select class='feeds'>";
$d_html="";
foreach($xml->channel->item as $news){
$d_html.="<div class='".$news->guid." common'>".$news->description.'</div>';
$select_html.="<option value='".$news->guid."'>" . $news->title . "</option>";
}
$select_html.="</select>";
echo $select_html;
echo $d_html;
?>

Show number of new records on page

I want to display the exact number of new entries (records) on my page of my phpmyadmin database after reloading the page. I looked online but couldn't find anything usefull. Maybe someone can help me. This is the code for the page:
<?php
header("Refresh: 120");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="verzoekstyle.css">
<title>Verzoekpagina DJ</title>
</head>
<body id="body">
<div id="tabel">
<?php
$con = mysqli_connect('database','username','password');
if (!$con)
{
echo 'Geen verbinding met server';
}
if (!mysqli_select_db($con, 'eventqy179_verzoeken'))
{
echo 'Database niet geselecteerd';
}
$sql = "SELECT * FROM info";
$query = mysqli_query($con,$sql);
if(!$query)
{
echo 'Onbekende fout gevonden';
}
echo '
<table class="table">
<tr>
<th><h3><b>Artiest</b></h3></th>
<th><h3><b>Titel</b></h3></th>
<th><h3><b>Verwijderen</b></h3></th>
</tr>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td><h3>'.$row['artiest'].'</h3></td>';
echo '<td><h3>'.$row['titel'].'</h3></td>';
echo '<td><h3><a href=delete.php?id='.$row['id'].'>Verwijderen</a></h3></td>';
echo '</tr>';
}
echo '</table>';
?>
</div>
</body>
</html>
Already found another way, just ORDER BY id DESC. Question is therefor answered

Reconciling Show/Hide JQuery Script into PHP

I currently have some php script that outputs results from a query. I would like to add at the end two buttons that will show/hide the final element, but am not sure how to do this.
The following is my php script:
while($result = mysqli_fetch_array($iname))
{
echo "<b>Event Name:</b> " .$result['EventName'];
echo "<br> ";
echo "<b>Location:</b> ".$result['Location'];
echo "<br>";
//this is where I would like to add my two buttons, that would show the "hidden" content when clicked
And here is what I have written in an HTML script, that I would like to reconcile into the PHP output:
<!DOCTYPE html>
<html>
<head>
<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js></script>
<script>
$(document).ready(function(){
$("#hidden").hide();
$("#hide").click(function(){
$("#hidden").hide(500);
});
$("#show").click(function(){
$("#hidden").show(500);
});
});
</script>
</head>
<body>
<button id="show">Show</button>
<button id="hide">Hide</button>
<p id="hidden">
Some Random Text that will be shown when the buttons are clicked
</p>
</body>
</html>
Any suggestions as to how this should be done?
How about if you get the number of result rows with $num_rows = mysql_num_rows($result);
Then, put a counter in your loop.
$counter = 1;
$theClass="";
while($result = mysqli_fetch_array($iname))
{
if ($counter == mysql_num_rows($result);){
$theClass="showHide";
}
echo "<div class='$theClass'>";
echo "<b>Event Name:</b> " .$result['EventName'];
echo "<br> ";
echo "<b>Location:</b> ".$result['Location'];
echo "<br>";
echo "</div>
$counter++;
}
Then, apply your javascript to the div whose class="showHide"

jquery tabs with mysql and pgp

I am trying to make tabs that contain information about student, these information is stored in the database. I want the first tab to show the information of all students. and the the second tab to show the information of the students that thier grade is A and the the third tab to show the information of the students that thier grade is B.
I made this code but just the first tab shows the information of the students but the second and third tabs don't show anything.
what is wrong with the code?
<html>
<head>
<title>students table</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
</head>
<body>
<div id="tabs">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<?php
$connection = mysql_connect("","","");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $connection);
$result = mysql_query("SELECT * FROM studentTable");
while($row = mysql_fetch_array($result))
{
?>
<div id="tabs-1">
<?php
echo "Student id: '" . $row["id"];
echo "Student name: '" . $row["name"];
echo "Student grade: '" . $row["grade"];
echo '</table>';
}
?>
</div>
<?Php
$result2 = mysql_query("SELECT * FROM studentTable WHERE grade = 'A'");
while($row = mysql_fetch_array($result2))
{
?>
<div id="tabs-2">
<?php
echo "Student id: '" . $row["id"];
echo "Student name: '" . $row["name"];
echo "Student grade: '" . $row["grade"];
echo '</table>';
}
?>
</div>
<?php
$result3 = mysql_query("SELECT * FROM studentTable WHERE grade = 'B'");
while($row = mysql_fetch_array($result3))
{
?>
<div id="tabs-3">
<?php
echo "Student id: '" . $row["id"];
echo "Student name: '" . $row["name"];
echo "Student grade: '" . $row["grade"];
echo '</table>';
}
?>
</div>
</div>
</body>
</html>
I think your issue is because you have your tabs-# <div> opening tags inside your while() loops.
$result = mysql_query("SELECT * FROM studentTable");
while($row = mysql_fetch_array($result))
{
?>
<div id="tabs-1"> //this here needs to be above/outside while($row = mysql_fetch_array($result)){
This is creating n number of <div id="tabs-1">,<div id="tabs-2">,<div id="tabs-3">, without matching closing tags </div>, so now <div id="tabs-2"> & <div id="tabs-3"> are nested in <div id="tabs-1"> and jQuery doesn't know which one to bind tabs to.
try moving them before the while() loops -
<div id="tabs-1">
<?php
$result = mysql_query("SELECT * FROM studentTable");
while($row = mysql_fetch_array($result))
{
...
}
?>
</div>
<div id="tabs-2">
<?php
$result2 = mysql_query("SELECT * FROM studentTable WHERE grade = 'A'");
while($row = mysql_fetch_array($result2))
{
...
}
?>
</div>
<div id="tabs-3">
<?php
$result3 = mysql_query("SELECT * FROM studentTable WHERE grade = 'B'");
while($row = mysql_fetch_array($result3))
{
...
}
?>
</div>
Also, you have echo '</table>'; at the end of each tab div. May want to remove those if you don't actually have a table.
In your script you are trying to use Jquery's tab method.
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
This is looking in the HTML for
<div id='tabs'>
You're tabs in here
</div>
Assuming that your SQL statments is not returing "NULL" then I would say you're issue is that you named you're div's tab-1, tab-2, tab-3. With ID's. This means that Jquery can't find anything to turn into tab's.
Change your jquery call to something like this
<script>
$(function() {
$( ".tabsclass" ).tabs();
});
</script>
and instead of using id's use classes.
<div class='tabsclass' id='tab-1'>
You're tabs in here
</div>
<div class='tabsclass' id='tab-2'>
You're tabs in here
</div>
<div class='tabsclass' id='tab-3'>
You're tabs in here
</div>

How to Load In Content with jQuery?

I am trying to add ajax functionality to my pagination so the content loads in the same page instead of the user having to navigate to another page when clicking the page links.
I should mention that I am using this php pagination class.
Being new to jquery, I am unsure of how to properly do this with the pagination class.
This is what the main page looks like:
<?php
$categoryId=$_GET['category'];
echo $categoryId;
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery_page.js"></script>
<?php
//Include the PS_Pagination class
include('ps_pagination.php');
//Connect to mysql db
$conn = mysql_connect('localhost', 'root', 'root');
mysql_select_db('ajax_demo',$conn);
$sql = "select * from explore where category='$categoryId'";
//Create a PS_Pagination object
$pager = new PS_Pagination($conn, $sql, 3, 11, 'param1=value1&param2=value2');
//The paginate() function returns a mysql
//result set for the current page
$rs = $pager->paginate();
//Loop through the result set
echo "<table width='800px'>";
while($row = mysql_fetch_assoc($rs)) {
echo "<tr>";
echo"<td>";
echo $row['id'];
echo"</td>";
echo"<td>";
echo $row['site_description'];
echo"</td>";
echo"<td>";
echo $row['site_price'];
echo"</td>";
echo "</tr>";
}
echo "</table>";
echo "<ul id='pagination'>";
echo "<li>";
//Display the navigation
echo $pager->renderFullNav();
echo "</li>";
echo "</ul>";
?>
<div id="loading" ></div>
<div id="content" ></div>
Marketing
Automotive
Sports
Any help on this would be great. Thanks.
$("a.category").live("click", showContent)
function showContent()
{
$("#content").load("/url/path/" + $(this).attr("id"));
}
will load either
/url/path/marketing, /url/path/automotive or /url/path/sports
into the content div

Categories