Clicked link submit value to SQL SELECT - javascript

I am trying to get this to allow the user to click on a link, which will then change the value of what is submitted in the SQL SELECT Statement, if that makes sense.
My Code so far:
<div class="Tabs" >
<?php
require 'database/connect.php';
$sql = "SELECT DISTINCT Week FROM PMWUpdates";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<a class="weeks"> Week' . $row{'Week'} . '</a>';
}
?>
</div>
<div class="Pages">
<?php
require 'database/connect.php';
$sql = "SELECT * FROM PMWUpdates WHERE Week='1'";
?>
</div>
I want the value of SELECT * FROM PMWUpdates WHERE Week="'1'"; to be what link the user has clicked on above, not just always 1. So if the user clicks on the link 2, the SQL changes to SELECT * FROM PMWUpdates WHERE Week="'2'";
I am basically trying to accomplish this, but instead, I want to display the number of tabs, based on the number of weeks in a database, and then, each week will display data from the database about that particular week.
Any help is appreciated.
Thank You

Your statement should be like this...
$sql = "SELECT DISTINCT Week FROM PMWUpdates";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<a class="weeks" href="$row['Week_link']"> Week' . $row['Week'] . '</a>';
}

If you mean dynamically changing data on a page which requires PHP to be processed, you have to take a look at AJAX. You can't do it this way, once your script is loaded, there is no more php, only HTML.
If you don't want to use AJAX, you will have to do it in 2 pages. First display links, then display your data related to whatever was selected.

The MySQL extension you are using is deprecated (>= 5.5). Use MySQLi or PDO instead.
What you are trying to accomplish is that where your "2" is listed, should change with the use of a variable. As you want to do it via Javascript, you will need a click event on those links which then submits the variable via AJAX.

Related

use jquery to return information from a php script that's running mysql

I know that title a bit confusing, so I'll try to explain better here.
I'm trying to create a form where a user inputs a ticket and that ticket gets assigned to a technician based off the service they provide. I have 3 text fields, username,email, Description of the problem. The next field is a select field. I populate this select field by running a php script that will query a database of services offered and return the results.
so something like
<select name="service' id= "Service" onClick="showTechnican()">
<?php include "services_offered.php"?>
</select>
When a user clicks on one of the services, I want the form to query my services_offered table and return the username(s) that provide that service in a div.
<div id="techs"></div>
I'm using a jquery script to do this
<script>
function showTechnician(){
var selectedValue = $("#Service").val();
if(selectedValue.length < 1){
document.getElementById("techs").inner.HTML = "Please Select A Service Above";
return;
}else{
var url = "technicians.php?q="+selectedValue;
$.get(url,function(data,status){
document.getElementById("techs").innerHTML=data;
});
}
}
I've also got the script technicians.php that is querying the database for me
<select name= "tech">
<?php
$q = $GET['q'];
$conn = new mysqli("localhost", "proxy_user",
"my*password","helpdesk");
if(mysqli_connect_errno()){
echo'Unable to connect to database:'.
mysqli_connect_error($conn);
}
else{
$query = "SELECT * FROM services WHERE Service_Descripton LIKE'%".$q.
"%';";
$result = mysqli_query($conn,$query);
if(!$result){
die("Invalid Query:" . mysqli_error($conn));
}
else{
while($row = mysqli_fetch_array($result)){
echo"<option value= "."{$row['User_Name']}>"."{$row['Service_Description']}".
'</option><br/>';
}
mysqli_close($conn);
}
}
?>
my issue is that when I click on a service offered, my div only shows a small empty box. Not really sure why it's not returning anything. I've got plenty of test data in the table. Made sure permissions were correct as well. I've checked the php_error_log and the query isn't failing. I'm not sure how to resolve this. Every other field on the form will insert just fine into the database.
I've attached a photo of the issue. See under "available testers" it just displays a little box.
photo of the problem
I figured it out. I can't spell, had a spelling mistake in the output of my query. Fixed it and everything worked as it should.

JavaScript PHP Dropdown Menu

I want to make a DropDown Menu, where someone can Choose an option out of several options. Ans depending on this Value the other Dropdown menu should be updated with values out a Database.
Like
Option 1: kind of sport
Option 2: Time
Option 3: Place
.
.
.
// EDIT adding actual option.
$query = 'SELECT * FROM kurs';
$result = $mysqli->query($query);
echo
"<table border = 1>
<tr align = 'middle'>
<td> <select>";
while ($zeile = $result->fetch_assoc()) {
echo "<option>" . $zeile['name'] . "</option>";
}
$result = $mysqli->query($query);
echo
"</td><td> <select>";
while ($zeile = $result->fetch_assoc()) {
echo "<option>" . $zeile['day'] . "</option>";
}
$result = $mysqli->query($query);
and if someone update choose one option out of 'day', the other option 'name' should change depending on the 'day' and vice versa.
I know how to get the information out of my SQL Database but i don't know how to make the Select Area dynamic. I'm not allowed to use JQuery.
Any help is appreciated
Summary: Use a <select onchange... event, XMLHttpRequest, and two PHP scripts.
To simplify the discussion, let's forget about Sport. The user first chooses a race date and then chooses an entrant name. Initially, the date dropdown shows all the dates, and the name dropdown is blank. After selecting a date, the name dropdown is populated with all the names for that date.
And suppose the database is so large that you don't want to send all the data to the browser in one go. You only want to send the names after the user has chosen a date.
This is classic AJAX. If you don't use jQuery (or another Javascript library), then you need to roll-your-own by using XMLHttpRequest (see http://www.w3schools.com/ajax ). That's what jQuery does.
Now, to examine your code. 'SELECT * FROM kurs' is going to give you all the records in the database. If you have 1000 records, your Date dropdown will have 1000 options! You might want:
SELECT DISTINCT [day] FROM kurs ORDER BY [day]
If you want the Date dropdown to feed names into the Name dropdown as soon as the user chooses a date, without the need to click a Submit button, then do this:
$optionlist = '<option value="">--Choose Race Date--</option>';
while ($zeile = $result->fetch_assoc()) {
$optionlist .= '<option>' . $zeile['day'] . '</option>';
}
echo "
<table border='1'><tr>
<td><select onchange='datechange(this)'>$optionlist</select></td>
<td id='tdSelectName'><select disabled></select></td>
</tr></table>";
The --Choose Race Date-- option is there so that if the user happens to want the first date then there will still be an onchange() event.
Your Javascript will need:
function datechange(sel) {
raceDate = sel.value;
if ( raceDate ) {
var myRequest = ...
// your synchronous XMLHttpRequest code goes here
document.getElementById("tdSelectName") = myRequest.responseText;
}
}
To see a fancier version of this in action, look at http://www.mostbryte.com/where_to_buy

Load more content with jquery & php

I'm trying to create a "load more" button to the news section of my website, but whenever I try anything my mind goes blank and I don't know how to begin making it.
I'm not good at any javascript/jquery/ajax just "some" php.
So far I only have my php script display all the news from my database...
$sql = mysqli_query($con,"SELECT * FROM news ORDER BY date DESC")
or die (mysql_error());
while($row = mysqli_fetch_array($sql)){
$usql = mysqli_query($con,"SELECT * FROM members WHERE id = 1");
while($user = mysqli_fetch_array($usql)){
echo('<li class=""><br/>('.$row['date'].')');
echo ('<a href="/news/'.$row['id'].'" title="'.$user['username'].'" rel="nofollow">');
echo ('<img src="'.$row['img'].'" alt="'.$user['username'].'" class="list_intros_img">');
echo ('<h4>'.$row['title'].'</h4>');
echo ('<p>'.substr($row['content'],0,400).'</p>');
echo ('</li><hr class="line">');
}
}
I suggest you to load a default number of news when you first load the page, map the "load more" button with an ajax request to fetch more news and update your list.
You could use the LIMIT and OFFSET keyword in MySQL to fetch more news. (https://dev.mysql.com/doc/refman/5.0/en/select.html).
In your case, that could be something like : "SELECT * FROM news ORDER BY date DESC LIMIT 10 OFFSET 10" to display news 11 to 20.
I hope this gives you a lead to get started.

dropdown list that refresh a div content upon select

i have totally no idea about how to do this, so i'm gonna just ask away.
I have a dropdown menu which list dates say
1/2/2013
2/2/2013
3/2/2013
4/2/2013
5/2/2013
6/2/2013
7/2/2013
if you were to select one of the dates, a div will pop out with say 5 choices
A
B
C
D
E
each choices are stored in the database, and if say B item is not available on 2/2/2013, i would have a script to disable it being selected. I've figured how to create that in php, but my ultimate question is
how do you select any of the dates but yet still able to retrieve the 5 choices from a database?
I'm currently doing something like this
function TheDisabler($aa)
{
global $con, $vdate;
$myresult = mysqli_query($con,"SELECT * FROM burger WHERE timeslot = '$aa' AND date = '$vdate'");
list($mycount) = mysqli_fetch_row($myresult);
if($mycount >= 1) {
echo "disabled";
}
}
but i figured that this works only once and if i were to change the date, the items within the div will not change =/
You should have a script that works every time the div element is clicked. So add an onClick event to div and then retrieve the required data from database on that event's handler.
if your table does not have large amount of data, create a php file that behave as if it's a javascript file like that:
<?php
$js = 'data = new Array()';
$sql = 'select ...';
$count = 0;
while($row = fetch_rows($sql)){
// do your check here
$js .= 'data[' . $row['date'] . '] = new Array()';
$js .= 'data[][' . $row['date'] . '][$count] . ' = "'.$row['item'] . '";';
$count++;
}
header('Content-Type: text/javascript');
echo $js;
?>
now you have a javascript array which have every date items, then on each click ask this array for that date key should return you the your options list
I think what you are looking for is the onchange event of the ckeck box. Each time there is a change in the drop down, call the function to retrieve the values from the database.

Execute script when select option is changed

I have a html form which includes a dynamically created dorpdown list. The dropdown list contains the names of newsletters which are stored in my MySQL database. What I want the dropdown list to do is: When I select a newsletter an other php script will activate which will take the data from the newsletter in my DB and writes it to a .txt file. The codes I currently have is:
The dropdown list:
<?php
echo "<select id=\"NieuwsbriefSelect\" name=\"show\">";
echo "<option size =30 selected>Select</option>";
if(mysql_num_rows($sql_result))
{
while($row = mysql_fetch_assoc($sql_result))
{
echo "<option value=\"$row[Titel]\">$row[Titel]</option>";
}
}
else {
echo "<option>No Names Present</option>";
}
?>
And the write script:
<?php
$title = $_REQUEST["show"];
mysql_connect('localhost','root','root');
mysql_select_db('NAW') or die (mysql_error());
$strSQL = "SELECT Content from NAW.Mail where Titel = '$title' ";
$sql_result = mysql_query($strSQL);
$row = mysql_fetch_assoc($sql_result);
$file = 'nieuwsbrief.txt';
$current = $row["Content"];
file_put_contents($file, $current);
?>
I do not want the page to redirect to the write script but just to execute it (I hope you get this^^). Is this possible using the HTML onChange Event or do I have to use javascript? Any help would be great and if you have a question about my code just ask in the comments!
NOTE!
I know I shouldn't be using Mysql_* and That I am vulnerable to sql injection but that is not the point.
You need to define a onchange event on your SELECT option and inside this onchange event, you can redirect to another page with selected item OR do an AJAX call to another PHP script
Unfortunately what you are asking is not possible without JavaScript. PHP is a server side language and therefore cannot be run, without a call from the client-side browser. You're best bet would be to use ajax in conjunction with the JavaScript onchange event. When the change event occurs, fire an ajax call off to the server to run the script with the selected option. This is the only way to do what you're asking without a page reload or redirect.

Categories