How to submit parameters by hyperlink-url and retrieve them in php? - php

Carrying values in a while cycle, each value is extracted from a SQL table, and I need to carry em in a since I'm making a list of items purchased from a specific client. I'm using this line for the href url:
echo "<table width=1200 style align=left>";
while($row3 = mysqli_fetch_array($rs3)){
$space = array("");
$text=$row3['description'];
$dash="_";
$url = preg_replace("![^a-z0-9]+!i", "_", $text);
$url= "".$url.".php";
echo "<tr><td colspan=2 style align=center><span class=font>" . $row3['relid'] . "</span></td><td width=132 style align=center><span class=font>" . $row3['invoiceid'] . "</span></td><td width=400 style align=center <span class=font><a href=$url>" . $row3['description'] . "</span></A></td><td width=132 style align=right><span class=font>" . $row3['amount'] . "</span></td><td width=132 style align=center><span class=font>Pendiente</span></td><td width=132 style align=left><span class=font>" . $row3['duedate'] . "</span></td></tr>";
}
mysqli_close($con3);
echo "</table>";
echo"<table width=1200>";
echo " <tr class=font>
<td width=1200 height=22 colspan=7 background=border-green-horizontal.png> </td>
";
I'm generating the url from the Description I extract from the database but I need to carry the Order ID, Client ID, etc from the specific row.
Each variable is needs to be carried to a form where the user will fill some fields regarding the product the client bought (Medical Test Results), and I need those variables to mark as "Done" so It won't show up again on the list.

Fix these syntax errors, report back if still broken.
<td width=400 style align=center <span class=font><a href=$url>" . $row3['description'] . "</span></a></td>
-->
"<td width=400 style align='center'><span class='font'><a href='$url'>" . $row3['description'] . "</span></a></td>"
EDIT:
Do you mean how to append a var to an url, so it can be retrived on the called page?
You can append the var to the url by echo "<a href='{$url}?id={$var}'>.
On the called page, you can read it by $_GET['id']

Related

Opening user profile in new tab

I am displaying proclamations in a table and I want to make each row clickable which I kinda did with js onclick function (i can only click on the name but i want to apply this to whole row). Now I want to see details about proclamations in new tab. My question starts here. I want to get proclamation ID when I click on a row but no idea how to do it. Plus i am doing onclick with js and rest of it is php so it's getting kinda messy there. I know it's not the best way to do it so i need your advice about how can I solve this issue.
P.S. I am not very familiar with js, those js parts been taken from web tutorials. And the code below is the section where I display the search results.
<section id="results">
<div class="container">
<table>
<tr>
<th>Name</th>
<th>Where</th>
<th>From</th>
<th>Date</th>
<th>Price</th>
<th>Type</th>
<th>PId</th>
</tr>
<?php
if(isset($_POST['search_btn'])){
include_once "db_connect.php";
$from = $_POST['from'];
$where = $_POST['where'];
$date = $_POST['date'];
$type = $_POST['type'];
$procid = $_POST['proc_id'];
if(empty($from) || empty($where) || empty($date) || empty($type)){
header("Location: index.php?search=empty");
exit();
}else{
$sql = "SELECT * FROM proc WHERE p_from = '".$from."' and p_where = '".$where."' and type= '".$type."' ";
$query = mysqli_query($conn, $sql);
$num_rows = mysqli_num_rows($query);
while ($rows = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
echo "<tr><td onclick='content(this)'>" . $rows['p_name'] . " " . $rows['p_surname']. " </td><td>" .$rows['p_from']. "</td><td>" .$rows['p_where']. "</td><td>" .$rows['p_date']. "</td><td>" .$rows['price']. "</td><td>" .$rows['type']. "</td></tr>" ;
}
echo "</table>";
}
}else{
echo "Error!";
}
?>
<?php
$fullUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(strpos($fullUrl, "search=empty") == true){
echo "<p class='error'>You did not fill in all fields!</p>";
}
?>
</table>
</div>
</section>
<script>
function content(elem){
<?php
?>
window.open('procdetail.php');
}
</script>
In your JavaScript function that calls window.open, you need to call the second parameter which will be the target to open the window, adding _blank will open it on a new tab instead of the same window:
window.open('procdetail.php', '_blank');
Give JavaScript the data needed to handle your information.
Also, if your idea is to open the tab with JavaScript with the content specially for the row, then you should try returning the necessary information to it, for example, if you want to open a new window with the content of a user, then you must let know JavaScript which user id should open.
This is as easy as returning in an attribute for your row element the value of the user id which data is being outputted by PHP.
Do something like this in your inline PHP script:
echo "
<tr class='table-item' data-userid='USER_ID_VARIABLE_HERE'>
<td>" . $rows['p_name'] . " " . $rows['p_surname']. " </td>
<td>" .$rows['p_from']. "</td>
<td>" .$rows['p_where']. "</td>
<td>" .$rows['p_date']. "</td>
<td>" .$rows['price']. "</td>
<td>" .$rows['type']. "</td>
</tr>
";
And then in your JavaScript code you could use an event listener for every element in your DOM with class="table-item":
window.onload = () => {
let tableItems = document.querySelectorAll(".table-item");
tableItems.forEach((element) => {
element.addEventListener("click", (event) => {
//Handle your click event here
let userId = event.target.dataset.userid; //Your user id
window.open('procdetail.php?id=' + userId, '_blank');
});
})
}
This will open in a new tab the contents of the url given which is procdetail.php?id=userId. So then, in your procdetail.php file you must handle the $_GET["id"] variable to show the respective data.
Additional resources:
You can read more of the window.open method here.
Try this
link name
If you want to continue to use inline JavaScript, you will first need to change the onClick attribute to be on the <tr> element instead of the <td> element. Once that is done, you can use the same function you have to pass the proc_id to the URL in the window.open(); method you have set in the function.
HTML/PHP
echo '<tr onclick="content(\''. $rows['proc_id'] .'\')">';
Javascript
function content(ID){
window.open('procdetail.php?proc_id=' + ID, '_blank');
}
This will make each of the table rows a clickable element that links to procdetail.php?proc_id=<YOUR_ID>
There is a way to perform this in HTML as well, but you want to modify the CSS of the table elements so that there are not non-clickable dead zones in the table row.
Using your current method of the single line echo, you could use something similar to the code below. This will append the proc_id data as a URL parameter for procdetail.php to parse.
Notice that the code only includes p_name and p_surname. You would have to add each definition with the same method.
PHP
echo "<tr><td><a href='procdetail.php?proc_id=". $rows['proc_id'] ."' target='_blank'>". $rows['p_name'] . "</a></td><td><a href='procdetail.php?proc_id=". $rows['proc_id'] ."' target='_blank'>". $rows['p_surname'] ."</a></td></tr>";
CSS
table tr td a {
display:block;
height:100%;
width:100%;
}
table tr td {
padding-left: 0;
padding-right: 0;
}
Check out this thread on the issue. It includes a usable demo that is hosted in JSFiddle.

Get Value Of Table Field

I am using php to echo query results out to a table like so
echo '<table border="1">';
echo '<tr>';
echo ' <th bgcolor="#FFFFFF" >Team Name </th>';
echo ' <th bgcolor="#FFFFFF" >Player Name </th>';
echo ' <th bgcolor="#FFFFFF">Jersey Number </th>';
echo '</tr>';
while ($Row = mssql_fetch_assoc($result))
{
echo '<tr><td>' . $Row['Team Name'] . '</td><td>' . $Row['Player Name'] . '</td><td>' . $Row['Jersey Number'] . '</td></tr>';
}
echo '</table>';
};
I want Jersey Number to be a hyperlink which can read the value of Player Name and pass that to a php file to be used as a query parameter and pull data for that specific player when the page loads.
Sample data is like
Team Name --- Player Name --- Jersey Number
Red Bob Jones 12
Blue Jack Horner 14
So if you click on 14, the code would read Jack Horner and store it in a variable. How is this achieved with php/html?
EDIT
Something like this is what I am after...using one page to redirect to and passing in the player name to use as a parameter.
Brief explanation, set the hyperlink to a page called other.php, and pass in the as part of the QueryString the value of .$row['name']
I tried the below syntax, but it gives me a 500 error
echo "<td><a href='other.php?playername=".$row['Player Name']'>" . $row['Player Name'] . "</a></td>";

php while loop echoing element outside of looped content

The forum pages on my website use PHP to create a table and then use a while loop to populate it from the database. This works fine and always has but I have tried to move the anchor, 'link', tag from around the post's title to the entire first section of the post within the table. To do this it goes through the following steps:
Open the table tag [OUTSIDE OF LOOP]
Echo headers [OUTSIDE OF LOOP]
Start WHILE loop that makes another post section for every post found.
Create table row
Create table data
Echo content
Close table data
REPEAT STEPS 5-7 ONCE MORE for post date section
Close table row
close table [OUSTIDE OF LOOP]
It should make the links clickable on all of the first section and they should be within the table like this:
<table> <--- *THIS IS BEFORE THE LOOP, IT GETS RUN ONCE ONLY* -->
<WHILE *do this like 5 times or something*>
<tr>
<a *category link*>
<td>
*content for the 'td' which is taken from the DB*
</td>
<td>
*content for the 'td' which is taken from the DB*
</td>
</a>
</tr>
<ENDWHILE>
</table>
However, in practice they end up outside of the table as can be seen in this screenshot:
Could anyone please explain this and how to fix it?
echo '<table class="forumTable">
<tr>
<th>Category</th>
<th>Last topic</th>
</tr>';
while($row = mysqli_fetch_assoc($catResult)){
echo '<tr>';
echo '<a href="category.php?id=' . htmlspecialchars($row['catID']) . '"><td class="catDesc">';
echo '<h3>' . $row['catName'] . '</h3>' . $row['catDesc'];
echo '</td>';
echo '<td class="catTime">';
$getTops = "SELECT topicID, topicSubject, topicDate, topicCat FROM topics WHERE topicCat = " . $row['catID'] . " ORDER BY topicDate DESC LIMIT 1";
$topResult = mysqli_query($connect, $getTops);
if(!$topResult){
echo '<p style="margin-top: 75px;">The last topic could not be displayed, please try again later.</p>';
}
else{
if(mysqli_num_rows($topResult) == 0){
echo '<p>No topics</p>';
}
else{
while($topRow = mysqli_fetch_assoc($topResult)){
echo '' . $topRow['topicSubject'] . ' at ' . $topRow['topicDate'];
}
}
}
echo '</td></a>';
echo '</tr>';
}
echo '</table>';
Since the source page confirms that the anchors are where you placed them, but the browser moves them around, you can either :
- contain your links inside the td table cell
- use an alternative approach to add the link where you want it html - table row like a link
Did you try to get page not from browser? How it looks?
I think browser does not allow you to put <a> into <table> directly without <tr><td> </td></tr>

PHP MySQL Database multi filtration

So I have a table with a whole load of properties and have displayed them on a webpage. As we have a wide range I developed a filtration system. However when used you can only select one tag, but we want to devise it in a way so that you can select one tag and then another. My current code is as follows.
<h3>Advanced Search:</h3>
<table>
<tr>
<td colspan="2" style="vertical-align: top; width:50%; !important;">
<h3>Location</h3>
<div>
Alsancak
Karsiyaka
Lapta
Kayalar
Sadrazamkoy
Cambeli
Baspinar
<!--
-->
</div>
</td>
<td style="vertical-align: top; width:25%;">
<h3>Number of Bedrooms</h3>
<div>
1
2
3
4
</div>
</td>
And it goes on with another few options the code then on the page is as follows:
<h1>Properties</h1>
<?php
$loc = $_GET["loc"];
$con = mysql_connect("localhost","Username","Password");
mysql_selectdb("db",$con);
$sql = "SELECT * FROM Properties WHERE Location='$loc' ORDER BY `Properties`.`Price` ASC";
$mydata = mysql_query($sql,$con);
echo "<h4>Location:" . $loc . "</h4><br>";
while($record = mysql_fetch_array($mydata)){
echo "<div id=\"property\">" . "<img src=\"images/" . $record['MainPic'] . "\" id=\"propimg\" align=\"left\">" . "<div id=\"text\" style=\"text-align: center; margin-right:20px;\"><h2>" . $record['Title'] . "</h2>" . $record['Ref'] . "<br/>" . "£" . $record['Price'] . "<br/>" . $record['Location'] . "<br/>" . "Details</div>" . "<img src=\"images/bottom-line.png\" id=\"bottomline\"></div>" . "</div>" ;
}
mysql_close($con);
?>
Any help would be greatly received. Thanks in advance!
I would suggest using the same page for displaying the results, whether the filter applies to the location or the number of bedrooms. You can always use WHERE (1 = 1) in your SQL so that you can append AND (Location='$loc') or AND (Beds='$bed'), though it would be preferable to use parameters.
Once you have that set up, you can modify your links. Let's say your destination page is webproperty.php. Store that URL in a variable $urlDest.
When you generate your links for filtering by location, add any variables in $_GET that aren't loc to the end of $urlDest, getting something like webproperty.php?bed=2. As you iterate through your locations, append the loc variable to $urlDest, like "<a href=\"$urlDest&loc=$loc\">".
Similarly, when you generate your links for filtering by bedroom count, add any variables in $_GET that aren't bed to the end of $urlDest, getting something like webproperty.php?loc=Alsancak. As you iterate through your bedroom counts, append the bed variable to $urlDest, like "<a href=\"$urlDest&bed=$bed\">".

Some html tags' parameters messup html/php code

When I create/edit post to my website, and when I use html tags (either type it by my self or using WYSIWYG editor (in this case nicEdit, but I've used different one - same problem)) it does not show anything on the page.
to be more specific: I have article container which imports php code, which than iterates through the db table and echo posts. This is the code:
<h3>Wydarzenia</h3>
<?php
//collects data from table "events" and sort it by "id" date in descending oreder
$event_table_data = mysqli_query($db_con, "SELECT * FROM events ORDER BY id DESC")
or die(mysqli_error());
//puts the data from "events" into an array
$event_content_array = mysqli_fetch_array($event_table_data);
//makes variables from table columns
$id = id;
$title = title;
$happeninng_date = happen;
$created_date = created;
$content = content;
$author = author;
//add new post button
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<a class='e_event_addpost_link' href='./index.php?link=add_post'><button class='e_event_addpost_button' type='button'>Dodaj nowy post</button></a>";
}
do{
//echo each event for main event article
echo "<span class='b_events_span' id='" . $event_content_array[$id] . "'>";
echo " <a class='b_events_anchor' id='" . $event_content_array[$id] . "'></a>";
echo " <h2 class='b_events_title'>" . $event_content_array[$title] . "</h2>";
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<form class='b_events_edit_form' name='edit_post' action='./index.php?link=edit_post' method='post'>";
echo " <input type='hidden' name='id' value='" . $event_content_array[$id] . "'> ";
echo " <input class='e_events_edit_submit' type='submit' value='Edytuj wydarzenie'>";
echo "</form>";
}
echo " <div class='fb-share-button' data-href='http://www.zhppgkaberdeen.co.uk/index.php?link=events#" . $event_content_array[$id] . "'></div>";
echo " <p class='b_events_happening_date'><i>Data wydarzenia: " . $event_content_array[$happeninng_date] . "</i></p>";
echo " <p class='b_events_content'>" . $event_content_array[$content] . "</p>";
echo " <p class='b_events_created_date'>Utworzono: " . $event_content_array[$created_date] . "</p>";
echo " <p class='b_events_author'><b>Czuwaj!</b><br/>" . $event_content_array[$author] . "</p>";
echo "</span>";
}while($event_content_array = mysqli_fetch_array( $event_table_data ));
?>
When I add tags like <img> for instance with alt="" parameter it completely ruins web page. What I mean by that is that the article container is not shown at all (I've checked that with firebug) as well as everything after the article tag (like footer for instance). What is even more strange is that this: alt="text" did not break the page but every other random word does, how ever it does not show the "text" at image at all.
Similar thing happen when I tried to create table within the post and title or target parameter, I cant remember which one.
I have tried to use htmlentities($event_content_array[$content]), htmlspecialchar($event_content_array[$content]) and mysqli_real_escape_string($do_con, $event_content_array[$content]) -> non of this helped.
Is there a way to fix that or I just need to give up using this tags/parameters?
Thank you for any help or explanation
if you are using e.g echo "<img alt="text">";try to escape the " such as alt=\"text\"

Categories