PHP $row['value'] in a div value - php

Alright so I am trying to basically take what's in $row['cardid'] and set it into the value of my div tag. Then on the html file this is echoing to, it would run my function which uses that value from the div tag. Right now when I use the onclick, it pulls up but says my value is undefined. So my question is why is my variable undefined when I pull it up using html?
<?php
$q=$_GET["q"];
$con = mysql_connect("*", "*", "*");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*", $con);
$sql="SELECT cardset, cardname, cardnumber, cardid FROM cards WHERE cardname LIKE '%".$q."%' OR cardset LIKE '%".$q."%'";
$result = mysql_query($sql) or die ('Error: '.mysql_error ());
echo "<table border='1'>
<tr>
<th>#</th>
<th>Cardname</th>
<th>Card Set</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['cardnumber'] . "</td>";
// This is the troubled line.
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.value)">' . $row['cardname'] . '</div></td>';
echo "<td>" . $row['cardid'] . "</td>";
echo "<td>" . $row['cardset'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

I don't fully understand your question, but you mean just something like this?
echo '<td><div onclick="changeimage(this.id)" id="'.$row['cardid'].'">' . $row['cardname'] . '</div></td>';
Use ID instead of value basically..
Example: jsFiddle

div elements doesn't have a value property, but you can get the value of the attribute named value - you should use .getAttribute("value"):
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.getAttribute(\"value\")">' . $row['cardname'] . '</div></td>';
An example: http://jsfiddle.net/vaSYh/
Also, if you need to store data in DOM elements, use data- attributes. (Or just name them that way even if you don't have to support HTML5 right now; It will make the transition easier)

Don’t use this.value.
It’s not a form.
Use this.innerText or this.innerHtml to get the content of the div

If I understand correctly your answer, you want to alert the value of $row['cardid']?
With your code, you can't get the correct value, because you're mixing up javascript code with php code.
The 'this' keyword in your 'onclick' event handler, is the javascript function itself, then it has not a 'value' property.
Try this code, to alert the right value:
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage('.$row['cardid'].'">'. $row['cardname'] . '</div></td>';

Related

Dynamically creating buttons that remove a row from a table

I'm trying to create a button for each row in my database that, when pressed, will delete this particular row. I should also mention that the data from the database is displayed correctly and the table I'm using is also completely fine.The buttons appear at the side of each row, when the button is clicked, the row dissapears but the data is not deleted from the database, when the page is reloaded the rows that were previously "deleted", reappear. After pressing the button i also get this "Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php:56 Stack trace: #0 {main} thrown in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php on line 56".
line 56 is : $del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);. The same query works fine when placed directly into phpMyAdmin.
<?php
// Check connection
include_once 'config.php';
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$sql = "SELECT * FROM records";
$result = $link->query($sql);
function post($key){ return(isset($_POST[$key]) ? htmlspecialchars($_POST[$key]) : ""); }
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if(post('rowButton'.$row['id']) =="Delete"){
$del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);
$deleted = '<p>Entry ' . $row['id'] . ' was succesfully deleted</p>';
}
else {
echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row["visitingdate"]. "</td>";
echo "<td>" . $row["department"] . "</td>";
echo "<td>" . $row["visitingreason"]. "</td>";
echo "<td>" . $row["importance"]. "</td>";
echo "<td>" . $row["visitorname"]. "</td>";
echo "<td>" . $row["company"]. "</td>";
echo "<td>". $row["internalrecipientname"]. "</td>";
echo "<td>". $row["visitinglocation"]. "</td>";
echo "<td>". $row["ETA"]. "</td>";
echo "<td>". $row["ETD"]. "</td>";
echo "<td>". $row["HRverification"]. "</td>";
echo "<td>". $row["visitcompleted"]. "</td>";
echo '<td><input type="submit" name="rowButton'. $row['id'] .'" value="Delete"/> </td>';
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
echo $deleted;
}
else { echo "0 results"; }
$link->close();
?>
First, this appears to be rather vulnerable to SQL injection attacks. StackOverflow is quite font of pointing this out up front, because it's really a solved problem that you should account for in the early stages of development. You're taking untrusted data (that was submitted by the user, without sanitizing it) and putting it directly in an SQL query. Bad things can happen when that occurs. Now with that aside, on to your actual question.
"Nothing happens" means the page doesn't change at all, right? So the browser doesn't know what to do when the button is clicked.
I think you haven't put any <form...> declaration here, which would be required for <input type="submit"> to do anything useful. You could use JavaScript with the stand alone submit button, but I don't see that in your code, either. You'll need something to tell the browser what to do when the submit button is pressed.
I haven't really tested the rest of your code, but based on what you've got already you might add the following:
else {
+ echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo "<tr>";
and
echo "</tr>";
+ echo "</form>";
}
(don't add the plus sign, that's just to show which line is added). I should add that I don't usually use submit buttons like this, so there's a chance I missed some additional details about how you're calling this, but putting the form in a <form> tag is at least a good start.
Edit
The mysql_query() function was removed in PHP 7; if you're using an older PHP you need to add support for the MySQL functions or if you're on PHP 7, you should use the MySQLi or PDO_MySQL functions instead. The warning box on the PHP manual page for mysql_query has some links for alternatives, how to select an alternative, and other supporting documentation that may help you. This StackOverflow answer may help, as well.

PHP script to make a link out of a MYSQL result?

So I made a test script for PHP output of a basic Mysql database. The problem is that I would like to make the auto increment called orderid to be able to become a link that I can click to open up a page that shows more info about that specific ID of that order.
It really seemed hard to find anything online and I have seen databases that have done this.
Also (not in this script) I have used CURRENT_TIMESTAMP but it doesn't put the current time, but does use the current date. What is the best way to get the current time (AM PM or 24 hour then convert it later) for MYSQL or PHP and input it?
Thank you for your help.
<?php
$con = mysqli_connect('localhost','user','password','database');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
else{
$sql="SELECT * FROM custinfo WHERE orderopen = 1";
$result = mysqli_query($con,$sql);
echo "<table border='1' bordercolor='blue' bgcolor='CCFFFF' cellpadding='0'>
<tr><h2> Open Orders:</h2>
<th>OrderID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><h3><font color='blue'>" . $row['orderid'] . "</font></h3></td>";
echo "<td><h3><font color='blue'>" . $row['firstname1st'] . "</font></h3></td>";
echo "<td><h3><font color='blue'>" . $row['lastname1st'] . "</font></h3></font></td>";
echo "<td><h3><font color='red'>" . $row['phone'] . "</font></h3></td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>
Well, if you just want to add an a element to this line:
echo "<td><h3><font color='blue'>" . $row['orderid'] . "</font></h3></td>";
Then just add an a element to it:
echo "<td><h3><font color='blue'><a href='somePage.php?id=" . $row['orderid'] . "'>" . $row['orderid'] . "</a></font></h3></td>";
Being a link doesn't make it particularly special, it's still just HTML like any other HTML that you're already outputting.

passing an undeclared reference through PHP

So currently, I'm making a music database online. The part I'm trying to implement is a playlist function so that given a list of your playlists, you can click one and view the music. What I have is this:
if($result = mysqli_query($link, $playlist))
{
if(mysqli_num_rows($result) > 0)
{
echo "<h1 style='text-align:center;'>Playlists</h1>";
echo "<table style='width:100%'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . '<a href="getPlaylist.php>' . $row['name']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
So I need it so that once someone clicks the link, it will transition them into getPlayList.php and also send over the name in the link, (technically $row['name']) so that I can actually pull up the information in the SQL database. I've tried doing things like:
echo "<td>" . '<a href="getPlaylist.php?link=' . $row["name"]. '">' . $row['name']."</td>";
but to no avail. Can anyone help?
edit:
So what this segment is supposed to do is, it makes a table with each row having a link to a different playlist, but they all go to "getPlaylist.php" where using the name of the link clicked, it will then retrieve all the songs that exist inside it.
edit: solved thanks to RamRaider
I had to use $_GET['link'] in getPlaylist.php

Read More Button

I have the below table in my site...
and what I want is that when i click on the read more link to redirect me to the page view_announcement.php and in this page to display me the whole data for this specific row.
For example, if we click on the link in the second row I want in the view_announcement.php to load all the data for this specific row.
My code in order to display this table is this...
<?php
$q = ($_GET['q']);
$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"project");
$sql="SELECT author,category,subject,content FROM announcements WHERE category = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Author</th>
<th>Category</th>
<th>Subject</th>
<th>Content</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
The view_announcement.php file doesn't contain any code yet because i dont know what to write.
One way to do it is to append a query variable to the "Read More" links. You'll probably need a unique identifier, such as an ID number, on your announements table. If you don't have one yet, I suggest adding one and setting it up to auto-increment.
You would want to modify your query to include the unique ID number:
$sql="SELECT id,author,category,subject,content FROM announcements WHERE category = '".$q."'";
Then you would modify the loop which prints your table out to include those unique IDs in the URL to view_announcement.php
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
And in your file view_announcement.php, you would make another SQL query to get the full data for a specific row, like this:
$sql="SELECT * FROM announcements WHERE ID = '".$_GET['id']."'";
If you click any button, that redirects to view_announcement.php file, where you can get the subject values.
Use that subject values in your query to get all the details which relates to that subject.

passing a link title to the next page

I'm trying to make automatically generated pages for users based on the username.
Basically I'm creating a table in HTML which displays all the users currently registered. The names are hyperlinks which redirect to a page in which I will post a welcome message.
The problem is that I don't know how to pass the name of the user I click on to the next page.
<table border='1'>
<tr>
<th>Username</th>
<th>Email</th>
</tr>
<?php
while($currentRow = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $currentRow['name'] . "</td>";
echo "<td>" . $currentRow['email'] . "</td>";
echo "</tr>";
}
?>
As you can see, I tried using get, but it won't work, I think it's because it doesn't know what I'm clicking on. The point is that I want the page with the welcome message say something like "welcome, $username". Any ideas?
You are closing the string with single quotes "'" that is why $_GET does not work. Replace profile.php?name='" with profile.php?name=" and also remove the single quote on the other end.
You have need remove single quote after ?name= and add it to before character >
Use
echo "<td><a href='/templates/profile.php?name=". $currentRow['name'] ."'>" .
$currentRow['name'] . "</a></td>";
Instead of
echo "<td><a href='/templates/profile.php?name='". $currentRow['name'] .">" .
$currentRow['name'] . "</a></td>";
try this in profile.php
Welcome <?php echo $_GET['name'];?>
In your profile.php
echo $_GET['name'];
echo "<td><a href='/templates/profile.php?name=". $currentRow['name'] ."'>" . $currentRow['name'] . "</a></td>";
close the single code after name value you are sending in a tag..
thats it.

Categories