I am currently stuck on how to use $_GET from a hyperlink and send it to another php page for processing.
I have 2 php pages. The first page, we'll call it page1.php, gets user input (customerID) from a HTML page through a textbox and according to that id, goes into the database and finds relative information such as the Customer Name and the Address etc using logical joins.
$id = $_GET['custID']; //This is to obtain the input from the HTML page.
I have that working perfectly.
My next task is to create another php page, we'll call it page2.php, in where i am required to output only the customerID and the customerName, and from there i am required to hyperlink every record in the Customer ID row which references to page1.php and when the user clicks any of those links, it should only show information according to that customerID in a table.
So far i can make each record in the Customer ID row link to page1.php but i am unable to output any results.
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["customerName"]?></td>
<td><?php echo "<a href='page1.php?'>{$row["custID"]}</a>"?></td>
</tr>
<?php }
Any help would be much appreciated.
Change the link to following
<?php echo ''.$row["custID"].'';?>
Related
i am new to PHP so please bear with me.
in my php project using mysqli queries i get results from mysql database and display them "as an html table with pagination" (e.g. 10 results per page).
my main piece of code to achieve that is the code below and works fine:
<table>
<tr>
<td> word-combinations </td>
</tr>
<?php
while ( $row=mysqli_fetch_assoc($result) ) { ?>
<td><?php echo $row['words'],$_POST['fname'];?></td>
</tr>
<?php } ?> <!-- end of while loop -->
<?php ?> <!-- the end of php tag -->
</table>
so the url of first page of the table is :
http://example.com/mytable.php?page=1
the url of second page of the table is :
http://example.com/mytable.php?page=2
and so on.
i also have created an start.php page which contains a "simple HTML form" with a "single input field" with POST action:
<?php
session_start();
echo "<form method='POST' action='http://example.com/mytable.php?page=1'>";
echo "<label for='fname'>First name:</label>";
echo "<input type='text' name='fname'> <br>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
?>
My goal is to be able to get (access) the string user entered in the input field of start.php page on all pages of table and append that string to the end of all results of the table pages when is displays to the user.
so to achieve this I've used php session concept.
when user fills out the input filed and hit submit button, user is redirected to http://example.com/mytable.php?page=1 url and the $_POST['fname'] value is present and is successfully added at the end of the results of the first page of the table.
when user fills out the input filed and hit submit button, user is redirected to http://example.com/mytable.php?page=1 url and the $_POST['fname'] value is present and is successfully added at the end of the results of the first page of the table.
but when user clicks on the same page1 pagination link (actually tag) under the table, the $_POST['fname'] is not present anymore !
also when user clicks on next pages links (page2, page3, etc) $_POST['fname'] is not present anymore !
My piece of code to create "pagination links" under table are:
<?php
for ($i=1;$i<=$total_pages;$i++){
echo "<a href='mytable.php?page=".$i."'> $i </a>";
}
?>
I don't know where I am wrong.
whether I need to place some code in somewhere to make session_start(); be executed on all pages of the table?
or for a reason which i don't know, the $_POST['fname'] can not be retrieved on all pages of the table.
any help would be appreciated.
The problem is that you are not setting the $_SESSION data after starting the session_start.
Your can try this..........But the Processor will be on the same page:.
1)create File to fetch the data....."index.html".....
<form action="index2" method="post">
2)Process the form When the user hit submit....."index2".....
<?php
session_start();
if(!empty($_POST['name1'])){
$_SESSION['name1']=$_POST['name1'];
}else{
echo "There is no Data in the Variable";
}
3)Now the SESSION variable will be absent in all the script you just have to start the session in each page, but i recommend using a one script processor page
Thanks to all guy who guided me.
so according to your guidance i change the form action from POST to default GET.
then according to CBroe guidance i added $_GET['fname'] into pagination link code.
in this way seems there is no need to deploy php sessions at all.
<?php
$userinput = $_GET['fname'] ;
for ($i=1;$i<=$total_pages;$i++){
echo "<a href='mytable.php?page=".$i."&fname=$userinput'> $i</a>";
}
?>
now everything works fine as expected but i don't know whether my codes and settings are perfect!
thanks to all
I am learning php and i need some help in one of my project.
How can i get id of a specific row while displaying the value of other column (in my case title) from a loop so that i can make it $SESSION and forward it to next page.
For now what i want to do is get an id of the row when i click the title and show the post in the next page and work further on it. I think i can use javascript for but don't know how to do so.
My codes (by which i am currently fetching data from database)
<div class="list-group">
<?php while($row = mysqli_fetch_array($search_result)):?>
<?php echo $row['title'];?>
<?php endwhile;?>
</div>
Thanks in Advance
Assume view_page.php is the page you want to view content of that row on when you click the row and you will use get method to extract id to go fetch content of a specific row in your database.
" class="list-group-item list-group-item-action list-group-item-dark">
on url address you will see something like after view_page.php?id=1
use this coode to get that row id: $id = $_GET['id'];
So on this application I'm working on, it searches for some code in my database, grabs it, opens a new page and places the code (where necessary) onto the new page by getting its id.
Now within that code (from the database) there is another form that is processed based on user's input. The problem that I am getting is that the new form is not processed on the same page. It redirects to a new url that doesn't specify the code from the database. For example...
When the new page is generated with the code from the database, the url looks like this...
localhost/newpage.php?id=1
Then when I submit the form within the code from the database it changes to this...
localhost/newpage.php?input1=blah&input2=blah
But I want something like this...
localhost/newpage.php?id=1&input1=blah&input2=blah
Just FYI this code needs to be dynamic. For example, let's say I don't know what id the user is looking for and within that id I don't know how many input fields there are.
If you guys need some explicit code (obviously I left out the unnecessary things)...
This is searchpage.php which searches db and displays all relevant items. Then the user selects an item which contains an id and generates newpage.php with that id...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
This is the form retrieved from database and placed in newpage.php...
<form action="newpage.php" method="GET">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
This is the php at the top of newpage.php when it is generated...
if (isset($_GET['id'])) {
//retrieves data from specified id from database
if(isset($_GET['inputs'])) {
//do something with user inputs
}
}
Is there any way to achieve this? All help is appreciated! Thanks :)
SOLUTION
For this to work, I had to create a session which stores the id. Consequently i had to change all my $_GET varaibles (that dealt with id info) to $_SESSION variables. But it works as long as you're not storing critical/sensitive info!
Here's the code changes...
Changes in search.php ...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
$_SESSION['current_id'] = $id;
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
Changes in newpage.php ...
<form action="newpage.php?id=" method="POST">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
Changes in php of newpage.php ...
if (isset($_SESSION['current_id'])) {
//retrieves data from specified id from database
if(isset($_POST['inputs'])) {
//do something with user inputs
}
}
ALL THANKS TO...
Sathik Khan
You can get the query string "id" in referred header. Either you can attach that query string in client or in server.
Please use $_SERVER['HTTP_REFERER'] to get referer url and from there you can get the id. If it's id of logged in user, I would recommend store them in session for security reason.
Based on your comments,
You can take any one of these actions,
1. Keep the ID in your form as hidden variable
2. Update your url with id as one of the query string and send back to client for error correction.
3. Keep the ID in session, if applicable
4. Use post and perform form validation before redirecting
Thanks.
I am trying to write a wordpress plugin and I have hit a bump. I am new to PHP (coded in Java before) and javascript so I am not sure whats the best way to solve my problem.
The Background
I have some data in a mySQL DB that I am using (each row has a unique ID and some other information I have added). I am able to search the DB using
$headss = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}costumesdb WHERE location = 'head'", ARRAY_A);
And display some of the information to the user using (this is one of 5 different drop-downs but they are all created in the same way)
Head: <select name="head">
<?php foreach ($heads as $head) { ?>
<option value="<?php echo $head['pieceName'] ?>"><?php echo $head['shopName'] . " - " . $head['pieceName'] ?></option>
<?php } ?>
</select>
For the moment I want the user to be restricted to choosing information that is already in the system.
The problem
The DB contains 2 pieces of information that the user does not need to know to fill in the form (a website URL and a picture URL). I need these 2 pieces of information once the form is submitted (I need to write some more code for that) to the server which spits out another page with the 2 URL's in it.
Whats the best way to send the data back to a PHP script? Am I able to access the row of data that the user has selected in the drop down and send the unique ID for that row back or do I need to do something else?
Edit:
This is the script that I am using to submit the code:
$('#createacostume').form({
success:function(data){
$.messager.alert('Info', data, 'info');
}
});
'
And then the page to display the information returned is:
$cname = $_POST['cname'];
$head = $_POST['head'];
echo "Data Returned Name $cname head $head
I think this is what you are asking:
User has to choose an item from a drop down and submit a form. You have to display the website URL and the image for that item in a second page. You want to know how this is typically accomplished.
If that's the case, you should pass the row id of the item to the second page like so:
<option value="<?php echo $head['ROW_ID'] ?>"><?php echo $head['shopName'] . " - " . $head['pieceName'] ?></option>
Then use the ROW_ID in the second page to access the data from the database and print out the website URL and the image.
Submit the first form (without the two field), INSERT the data into the database, get the ID of insert.
Pass the ID to the next page which would set the ID into a hidden form field (or GET or POST parameter, plenty of choices) of the new form (with the two fields and just UPDATE the database upon submitting the second form.
If you like to show the original data in the second form, just pull the data from the database and use it to render the form instead of passing just the ID into a hidden field.
I am trying add a "Delete" button in my application. The button's functionality is to remove the database row when it is clicked. I guess I will need to create something like delete.php and link the button to it. But I have no idea how to do it. Can anyone help?
Below is my code:
<table id="edit_accounts" class="tablesorter">
<thead>
<tr><?php
while($v=mysql_fetch_field($result)) if($v->name!="paid_for_year_date" && $v->name!="approved"){
?><th class="header"><?php echo display_version($v->name);?></th>
<?php
}
?>
<th class="header">Actions</td>
</tr>
</thead>
<?php
while($row=mysql_fetch_assoc($result)){
$row["category"]=$categories[$row["category"]]["category_name"];
$pfydate=$row["paid_for_year_date"];
unset($row["paid_for_year_date"]);
$extra_link="";
if($pfydate==$row["join_date"]){
$extra_link="<br/><a href='mark_as_paid.php?account_id=".$row["account_id"]."&auth_code=".md5("lgotadmin".$row["account_id"])."'>Mark as Paid</a>";
}
if($row["approved"]==0){
$extra_link.="<br/><a href='approve.php?account_id=".$row["account_id"]."&auth_code=".md5("lgotadmin".$row["account_id"])."'>Approve</a>";
}
unset($row["approved"]);
?>
<tr><td><?php echo implode("</td><td>",$row);?></td><td>Edit Account<br>Edit Transactions<br/>Edit Account<?php echo $extra_link;?></td></tr>
<?php
}
?>
</table>
Follow these steps
1) You need to create a file deleteFile.php (or) you can create a single file and do all the operations like insert, update and delete by using if conditions.
2) Then, you should pass the row id or some identifier to the page to identify which row has to be deleted.
3) In that, you should write a query to delete the row by using this identifier.
4) Then you can give an alert msg and redirect into the page.
5) In the case of single file, you need to send an operation type like delete with the id to the page
You didn't mention how do you want to update your page. There are essentially two way to refresh the page.
Refresh the entire page.
In this you will call your delete.php with some parameters. In delete.php you perform the delete operation based on given parameter and then send it back to your code responsible for genering your page. The newly generated page will not have deleted row.
AJAX
If you are using AJAX based call to delete item
This becomes little bit more complex on browser related code.
- You pass id/parameters to delete.php via AJAX call.
- delete.php deletes row and sends status message back to browser.
- You javascript handles the response and performs required DOM manipulation to delete row from table in case of successful operation.