How to get text from hyperlink? - php

Hello i'm new in php so i want to know if there's a way to get the text from a hyperlink that has been created by a loop?
is there any solution? my problem is that i can't get the name from the hyperlink so that i will pass that name to other page .
// i have this code which doesn't work.
<head>
<script language="javascript" type="text/javascript">
function GetText()
{
var linktext=document.getElementById('testlink').innerHTML;
<?php $_SESSION['name'] = "<script>document.write(lintext)</script>"?>
}
</script>
<title>Search</title>
<link rel="stylesheet" type="text/css" href="admincss.css" />
</head>
if($pic != "")
{
$total=mysql_affected_rows();
for($ff1=0;$ff1<$total;$ff1++)
{
print "<table width='100%' align='center' border='5'>";
print "<td width='20%'><img src=\"$pic\" height='100px' width='100px'/> </td>";
print "<td align = 'center' width='20%'><a onclick='GetText()' ' href='search.report.php' id='testlink'>$name</a></td>"; //
print "<td align = 'center'width='20%'>$bday</td>";
print "<td align = 'center'width='20%'>$no</td>";
print "<td align = 'center'width='20%'>$eadd </td>";
print "</table>";
}
}

I hope you are talking about this line:
print "<td align = 'center' width='20%'><a ' href='search.report.php' id='testlink'>$name</a></td>"; //
You need to write this line as below:
print "<td align='center' width='20%'><a href='search.report.php?name=$name' id='testlink'>$name</a></td>"; //
And then you can get the name on search.report.php file by this code:
$_SESSION['name'] = $_REQUEST['name'];
echo $_REQUEST['name'];
You cann't execute the PHP code in the JS file. so you need to send it on server as PHP is server side language.

Related

Add selected data from one table to another table using 'onclick button HTML

My project is about Allergies and I am trying to add the allergies given in one table "List of Allergies" to another table "MyAllergies" with an onclick button "add" for each allergies given. What I want is when the user clicks on add button for one allergy, that specific allergy should automatically be added to the "MyAllergy.php". Please help me. I have been trying for 2 days and I got no where. However, I tried the query and it was almost correct. I would be so grateful. Thank you. Let me know if you want me to attach the other code or any more questions. :)
AddAllergy.php
<?php
include('Session.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Allergen Details</title>
<div align="center">
<h1>Allergen Information</h1>
</div>
</head>
<body>
<?php
$allergysql = 'SELECT Allergy,Description,Symptoms FROM FoodAllergy';
$retrieve = mysqli_query($connection, $allergysql);
if(!$retrieve)
{
die("Could not get data!".mysqli_error($connection));
}
print "
<table border=\"5\" cellpadding=\"7\" style=\"border-collapse: collapse\" bordercolor=\"#85144b\" width=\"100%\" id=\"AutoNumber2\" bgcolor=\"#7FDBFF\"><tr>
<th width=100>Allergy:</th>
<th width=100>Description:</th>
<th width=100>Symptoms:</th>
<th width=100>Status:</th>
</tr>";
while($rowA = mysqli_fetch_assoc($retrieve))
{
print "<tr>";
print "<td>" . $rowA['Allergy'] . "</td>";
print "<td>" . $rowA['Description'] . "</td>";
print "<td>" . $rowA['Symptoms'] . "</td>"; ?>
<td> <div align ="center"> <button type ="button" onclick="addallergy()">Add</button></div></td>
<script>
function addallergy() {
// WHAT SHOULD I ADD HERE?? //
alert ('Added to your profile!');
}
</script>
<?php
print "</tr>";
}
print "</table>";
?>
</body>
</html>

How to update a row in a field one at a time on php?

I have a problem on updating the status field on tblitemlist. I wanted to display all status = 'PENDING' and when it's done, what I want to do is update its status to finished with just clicking a button. I wanted it one by one and not clicking a button and update everything at the same time. Please help me
here's what i did:
$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED'") or die(mysql_error());
I tried this one. There's no error but when I checked my database, the status is not changing.
$id=$_GET['id'];
$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED' WHERE id='$id'") or die(mysql_error());
Gonna show my codes..
inner.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
include("../connection/connection.php");
//
if(!isset($_SESSION['u']))
{
header("location../resto/index.php");
$result = mysql_query("SELECT tblcustomer.fname, tblitemlist.item, tblitemlist.category, tblitemlist.date_ordered FROM tblitemlist INNER JOIN tblcustomer ON tblcustomer.cust_id=tblitemlist.cust_id WHERE tblitemlist.status='PENDING' ORDER BY tblcustomer.cust_id" ) or die(mysql_error());
echo "<table width='900' border='1' align='center'>";
echo "<tr>";
//echo "<td>Code</td>";
echo "<td align='center' style='font-size:20px;'>Name</td>";
echo "<td align='center' style='font-size:20px;'>Item</td>";
echo "<td align='center' style='font-size:20px;'>Category</td>";
echo "<td align='center' style='font-size:20px;'>Date</td>";
echo "<td align='center' style='font-size:20px;'>Status</td>";
echo "<tr>";
while ($row=mysql_fetch_array($result))
{
echo "<tr>";
//echo "<td>" .$row['code'];
echo "<td>" .$row['fname'];
echo "<td>" .$row['item'];
echo "<td>" .$row['category'];
echo "<td>" .$row['date_ordered'];
?>
<td>DONE</td>
<?php
echo "<tr>";
}
echo "</table>";
}
?>
</p>
</body>
</html>
update.php
<?php error_reporting(0); ?>
<?php
include ("../connection/connection.php");
$id=$_GET['id'];
$query_update = mysql_query("UPDATE tblitemlist SET status='FINISHED' WHERE id='$id'") or die(mysql_error());
if($query_update){
echo '<script type="text/javascript">alert("COOKED");
window.location="index.php";
</script>';
}
else{
echo '<script type="text/javascript">alert("UNSUCCESSFULL");
window.location="index.php";
</script>';
}
?>
You have to add WHERE statement to specify which table item has to be updated. First you have to provide the query with the ID. For example, each button would post the 'id_to_update' through a form.
$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED' WHERE id = ".$_POST['id_to_update']) or die(mysql_error());
Is that what you want ?

How to use $_Get to store its values in a database?

I want to display all users that a "super" user is responsible for. Then the "super" user can send a comment to any of the users by clicking on their user id.
The first part of showing all the users is working fine! My problem is in sending a comment by the super user. I want to store the user id (ie suid) in the database. So I think to use the $_Get function. But when I tried it, nothing stores in the database. I think I made a mistake in it. So can anyone help me on that?
This is the code that will show all users to the super user:
<?php
session_start();?>
<?php
require("noCache.php");
$uid=$_SESSION['uid'];
?>
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Send a comment</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/layout.css" type="text/css" media="all">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script type="text/javascript" src="js/jquery-1.4.2.js" ></script>
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_400.font.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_700.font.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_600.font.js"></script>
</head>
<body>
<div class="main">
<header>
<div class="wrapper">
<h1> Biz</h1>
</div>
<nav>
<ul id="menu">
<li class="alpha"><span><span>Home</span></span></li>
<li><span><span>About</span></span> </li>
<li><span><span>Projects</span></span></li>
<li><span><span>Contacts</span></span></li>
<li class="omega"><span><span>Services</span></span></li>
</ul>
</nav>
</br></br>
</header>
<head>
<!-- CSS Stylesheet -->
<style type="text/css">
html{
}
body{
text-align:center;
}
</style>
</head>
<?php
$dbh=mysql_connect("localhost", "root", "hahaha1") or die (mysql_error());
mysql_select_db ("senior");
$result = mysql_query("SELECT * FROM sensorusers where uid=$uid");
echo "<html><body>";
echo "<table cellspacing=10 cellpadding=5 ><tr> <th>ID</th><th>Name</th><th>Username</th><th>Password</th><th>Date of Registeration</th><th>Phone</th></tr>";
while ($row = mysql_fetch_array($result))
{ echo "<form method='post' action='sendcomment.php'>";
echo "<tr><td><input type='submit' name='suid' value='".$row['suid']."' /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['dusername'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['phone'] . "</td></tr></form>";
$_GET['suid'] = $row['suid'];
}
echo "</table></body></html>";
mysql_close($dbh);
?>
<html>
<body>
<!-- CSS Stylesheet -->
<style type="text/css">
body {
font-family:"Andalus"
font: 16px ;
color:black;
padding: 30px 5px;
text-align: center;
}
</style></body></html>
Here is the part that I want to ask about:
echo "<table cellspacing=10 cellpadding=5 ><tr> <th>ID</th><th>Name</th><th>Username</th><th>Password</th><th>Date of Registeration</th><th>Phone</th></tr>";
while ($row = mysql_fetch_array($result))
{ echo "<form method='post' action='sendcomment.php'>";
echo "<tr><td><input type='submit' name='suid' value='".$row['suid']."' /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['dusername'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['phone'] . "</td></tr></form>";
$_GET['suid'] = $row['suid'];
}
Solution:
Thank you all for helping. I fixed the code. I used method post to save the value of suid the first time. Then I print it as a hidden input. Finally I save the value of the hidden input in the sql and it works!
you have POST I DONT SEE GETin your code , and its depend on your html inputs (u didnt post them)if u make method GET or POST.
if your method is GET then you use GET
but try use this
if(isset($_POST['submit'])){
instead of
if($submit){
EDIT .
you are using method='post'
then you must use POST not GET
EDIT2:
you have an error in your sql , you used VALUE and it should be VALUES
instead of this
$place="INSERT INTO comments VALUE
replace it by
$place="INSERT INTO comments (columns here ,..., ,..) VALUES
How to use $_GET:
Put the data in the URL like so:
sendcomment.php?suid=blahblahblah
Then you can use $_GET, like so:
$suid = $_GET['suid']
Currently your code uses POST to send suid
Or change your form method.
echo "<form method='post' action='sendcomment.php?suid=".$row['suid']."'>";
There are multiple issues in your code. I will try to list the biggest ones, but may still miss some.
Code 1 -
Issue 1-
You have multiple <html></html>,<head></head>, and <body></body> blocks.
<html>
...
<head>
...
</head>
<body>
...
<head>
...
</head>
...
echo "<html><body>";
...
echo "...</body></html>";
...
<html>
<body>
...
</body>
</html>
Issue 2-
Your <form> is around a <td> block, which is invalid, so it will not POST properly. It either needs to be around the table <form><table></table></form>, or inside a cell <td><form></form></td>.
echo "<table cellspacing=10 cellpadding=5 ><tr> <th>ID</th><th>Name</th><th>Username</th><th>Password</th><th>Date of Registeration</th><th>Phone</th></tr>";
while ($row = mysql_fetch_array($result))
{ echo "<form method='post' action='sendcomment.php'>";
echo "<tr><td><input type='submit' name='suid' value='".$row['suid']."' /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['dusername'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['phone'] . "</td></tr></form>";
$_GET['suid'] = $row['suid'];
}
echo "</table></body></html>";
Issue 3-
You are misusing $_GET and it is outside your <form></form> tags. $_GET is used to get value from the url.
$_GET['suid'] = $row['suid'];
Code 2-
Issue 1-
You are setting $id1=$_GET["suid"];, but in your form in Code #1, you are using method="post".
$id1=$_GET["suid"];
Issue 2-
When you are posting from Code #1 to Code #2 (<form><input type='submit' name='suid' value='".$row['suid']."' /></form>), you are only sending name='suid', so all your $_GET & $_POST vars will not be set to be used in the INSERT-
$id1=$_GET["suid"];
...
$name=$_POST['sent_by'];
$id=$_POST['hidden_id'];
$id2=$_POST['hidden_id2'];
$message=$_POST['message'];
$submit= $_POST['submit'];
...
Issue 3-
You have errors in you query. It should be VALUES, not VALUE, you should include the columns before `INSERT INTO comments (id,name,message,receiver,sender,date,time) VALUES ('','$name','$message','$id','$id1','$date','$time')
$place="INSERT INTO comments VALUE('','$name','$message','$id','$id1','$date','$time')";
$run = mysql_query("$place");
Issue 4-
All your value= in your form is missing the quotes '' around the values.
<form action="sendcomment.php" method="post">
<input type ='hidden' name='hidden_id' value = <?php print $uid; ?> >
<input type ='hidden' name='hidden_id2' value = <?php print $id1; ?> >
Name : <input type ='name' name='sent_by' value = <?php print $username; ?>></br>
Message: <textarea name='message'></textarea></br>
<input type='submit' value='submit' name="submit" /></br>
</form>
I know that you have accepted an answer, but you have a lot of code issues to still address. Each of these will cause you issues with your form and inserting into your database. Plus they will cause you more issues.
Just simply this
$id1=$_GET["suid"];
replace with
$id1=$_POST["suid"];
When using $_GET varible shuld be in the url.
When using $_POST variable isn't shown in url

How can we create custom style for Jquery-ui dialog window in PHP?

I am having a PHP page which the result table contain a link that opens a popup box. Earlier I used JavaScript. But I want to hide the address bar, so this cant be done in JavaScript(hope so). So I tried using jQuery-ui for this.
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css">
<style type="text/css">
#data-specs {
border-collapse: collapse;
}
#data-specs th,
#data-specs td {
padding: 0px;
border: 0px;
}
.loading {
position: absolute;
top: 50%;
left: 50%;
margin-top: -8px;
margin-left: -8px;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var $loading = $('<img src="loading.gif" alt="loading" class="loading">');
$('#data-specs a').each(function() {
var $dialog = $('<div></div>')
.append($loading.clone());
var $link = $(this).one('click', function() {
$dialog
.load($link.attr('href'))
.dialog({
title: 'Dialog Title',
width: 500,
height: 300
});
$link.click(function() {
$dialog.dialog('open');
return false;
});
return false;
});
});
});
</script>
</head>
My table part code is like this:
print "<table width='875' id='data-specs' align='center'>";
while($row = mysql_fetch_array($result))
{
print "<tr height='18'>";
print "<td width=200 align=left style='padding-left:10px'>" . $row['Country'] . "</td>";
print "<td width=70 align=center>" . $row['MidEstimate'] . "</td>";
print "<td width=70 align=center>" . $row['LowEstimate'] . "</td>";
print "<td width=70 align=center>" . $row['HighEstimate'] . "</td>";
print "<td width=118 align=center>" . $row['Source'] . "</td>";
print "<td width=110 align=center>" . $row['StudyLocation'] . "</td>";
print "<td width=89 align=center>" . $row['Relevance'] . "</td>";
print "<td width=89 align=center>" . $row['Quality'] . "</td>";
print "<td width=61><a style='color:#E46D0A;' href='popupboxD.php?SId=$vv'>".$row['Info']."</a></td>";
print "</tr>";
}
}
if(empty($result)){
print "<table width='875' align='center'>";
print "<tr height='1'><td colspan='9'><font color='#000080'><b>Does not have information on this particular selection.</b></font></td></tr>";
print "</table>";
Now the problem is its all works well. But when I click the link, the jQuery dialog box opens and the style(css) for my parent window is also changing? I want the style to be applied only for dialog window also I want to change the look and feel of dialog window? How can I do this? please help me in this.
Update
I am using this code in drupal 6, but when I click the link the pop-up is not opening as a modal dialog window. IT opens completely in the parent window? How can I in-corporate jQuery UI in drupal 6 with same code? Please help me.
To make your own jQuery ui styling (a theme), make your changes on jquery ui themeroller. After that you can download it (before you should deselect all components), if it's a zip decompress it, save the files on your server and replace the href attribute of you following line:
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css">
But this will change all jQuery ui elements of this page.
=== UPDATE ===
To make the dialog modal you have to add the modal option.
.dialog({
...
modal: true
})
Also see my updated jsfiddle.
In general, PHP has nothing to do with JQuery, Javascript, HTML controls and all that stuff.
PHP is merely a text processor. It is your job to determine, what text to output. PHP won't do it for you.
Once you done with text - then you can print it out with PHP. It is not a big deal too - there are simple formatting rules for the PHP strings.
However, to output large amounts of texts. you can just escape from PHP:
<?
//some PHP
?>
<table width='875' align='center'>
<tr height='1'>
<td colspan='9'>
<font color='#000080'>
<b>Does not have information on this particular selection.</b>
</font>
</td>
</tr>
</table>
<?
//PHP again
?>

How to color even/odd table rows in PHP

OK I have a question I have this code which I will list below. I need to make the even rows a light blue and the odd a white. Now it doesnt show up so I am assume I am doing soemthing wrong. Now do I need to do the order in it so that way the rows will look the way I need it to do?
<html>
<head>
<title> Html Tables</title>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
if ($rowcount%2==0){
echo "</tr>";
echo "<tr bgcolor=\"#5CCDC9\">\n";
}
else{
echo "</tr>";
echo "<tr bgcolor=\"#FFFFFF\">\n";
}
$rowscount++;
}
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>
ok I am trying to understand this better after reading a few things this is my new code
<html>
<head>
<title> Html Tables</title>
<style type=<\"text/css\">
.even { bgcolor:#5CCDC9; }
.odd { bgcolor:#FFFFFF; }
</style>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
if ($rowcount%2==0){
echo "</tr>";
echo "<tr class=\"even\">\n";
}
else{
echo "</tr>";
echo "<tr class=\"odd\">\n";
}
$rowcount++;
}
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>
now i just dont understand how to write it in PHP I am reading and trying to figure out how to make sense of it. Sorry I am a newbie at this.
You are using $rowcount in your conditional and initialization but you are using $rowscount (with in "s") in your incrementing.
Note: you should really be using CSS for that rather than the bgcolor property.
you have a typo...change $rowscount++ to $rowcount++
The easiest way to do this is with CSS. You can use the nth-child rule to select odd and even rows of the table and colour them differently. That way you don't need the modulus operator if statement you're using.
See this fiddle for an example.
Try this. It's a little bit cleaner and it works:
<html>
<head>
<title> Html Tables</title>
</head>
<body>
<table width="50%" cellpadding="2" cellspacing="2" border="1">
<?php
for($x = 1; $x <= 12; $x++) {
if ($x % 2 == 0) {
echo ' <tr bgcolor="#5CCDC9">', PHP_EOL;
} else {
echo ' <tr bgcolor="#FFFFFF">', PHP_EOL;
}
echo ' <td align="center" style="width:100px">' . $x . '</td>', PHP_EOL;
echo ' </tr>', PHP_EOL;
}
?>
</table>
</body>
</html>

Categories