how to print a php page with table data - php

I have a search results in a form which displays a particular persons details.
Here what I need is I should have a print page button and print that search result.
How is that posible in PHP?

You can use the file -> print from the browser and use a CSS print
If you want a button to print on the website, you will have to use Javascript
<form><input type="button" value=" Print this page "
onclick="window.print();return false;" /></form>

Related

Mulitple event trigger different PHP file at the same form

I have this code:
echo "<form action='activity1.php' method='post'>";
echo "<input type='checkbox' name='checkbox_test[]' value='1'>aaa";
echo "<input type='checkbox' name='checkbox_test[]' value='2'>bbb";
echo "<input type='checkbox' name='checkbox_test[]' value='3'>ccc";
echo "<br><br>";
echo "<input type='submit' name='activity1' value='Activity1'>";
echo '</form>';
This will results 3 checkboxes and 1 summit button. The selection will be handled by acvitity1.php.
I would like to add another submit button for each checkbox line something like this:
echo "<form action='activity1.php' method='post'>";
echo "<input type='checkbox' name='checkbox_test[]' value='1'>aaa "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='2'>bbb "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='3'>ccc "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<br><br>";
echo "<input type='submit' name='activity1' value='Activity1'>";
echo '</form>';
If the user press the activity2 buttons, how can i pass the value another php file (for ex activity2.php) ?
So how can I put a form into another form ?
Think about a table / form where you can select any line for delete (activity1), and buttons for the end of each line to edit the table row where the button has pressed (activity2).
Thank you!
Post edit:
As I am unable to comment, I was unable to ask for clarification. You said:
Ammadu: after pressing activity2 buttons the page get (needs to be) redirected to another page (activiy2.php). At activity2.php i want to catch checkbox_test[] value with $_POST.
In that case, AFAIK, you can not explicitly redirect the request to activity2.php as form nesting is not allowed and "Activity2" submit buttons will always POST to activity1.php. The simplest thing you can do is check which submit button POST-ed the request and react acorrdingly (code for checking the POST variable is shown below in the pre-edit section).
Pre-edit:
Your questions seems a little bit unclear to me, but I'll try to answer it based on what you wrote at the end of the question:
Think about a table / form where you can select any line for delete (activity1), and buttons for the end of each line to edit the table row where the button has pressed (activity2).
Also, I am no professional, merely a student, and an amateur programmer.
I had a similar problem while attending web programming course at my college. Specifically, there were table rows with checkboxes at the end of the each row for deleting the rows and the "Edit" button next to each of them. There was a "Delete" button below the table that was used to call the script that would remove the rows that were marked for deletion. We were using some dirty, dirty hacks to make that work.
You asked about form nesting, quick Googling revealed that form nesting is not a valid code.
Unless you really need to do your tasks in a separate PHP script I would suggest checking the POST variable to see which button was used to POST the form data to the server:
<?php
if (isset($_POST['activity1'])) {
//code for activity1 button
}
elseif (isset($_POST['activity2'])) {
//code for activity2 buttons
}
?>
This approach is also causing another problem - there is no easy way to identify which row that button belongs to. What you can do is dynamically name the buttons in the process of creation for each row (activity2_1, activity2_2...) and then create a loop in the PHP script that would check which button was clicked, which is a very ineffective way of doing things. That was the dirty hack I used back when studying WP course.
What I would go for are simple anchors. You can create them inside a PHP loop like this:
<?php
//...rest of the code in the loop...
echo 'Edit';
//...rest of the code in the loop...
?>
The script activity2.php should then perform a simple GET check and do the rest of the job:
<?php
if (isset($_GET['rowID'])) {
//activity code
}
?>
If you really need to use the buttons:
You can style the anchors using CSS to look like and behave like buttons and then use the code shown above;
...or, if you are allowed, you can use simple JavaScript code that you can generate inside the PHP loop in the same manner, like this (out of my head) and then echo it:
<?php
//...rest of the code in the loop...
echo '<input type="button" onclick="location.href="/activity2.php?rowID=' . $your_row_id . '";" value="Edit" >';
//...rest of the code in the loop...
?>
Hope this helps.

Print to paper selected data

I've a database (image1) and using PHPMaker i created the graphic interface (image2). What i want is to add a button at the bottom of the page that will print the selected data from the selected fields (image3).
Can anyone tell me how to do that?
Do i have to use php code or a javascript?
Just add a print link by echoing the following to create a "Print" link on the page
echo "<a href='#' onclick='javascript:window.print();'>Print Page</a>";
Button click will make the browser present the user with the default print dialog.

Retain the page update by Javascript and run the PHP code?

In a php project I need to add items to database, list them & allow the user to edit & update items using a single page.
This is my code to edit item link in HTML table
echo ' Edit ';
When a user click on the above link I need to hide Add button and display two new buttons to Update & Cancel the edit + display selected item name in a Text box to Edit.
To hide and display buttons I'm using jQuery and to display the item name i need to use PHP.
Here when i put PHP code and reload the page with $_SERVER['PHP_SELF'] (as in above code), hiding & displying of buttons is lost after the page load. (If I remove the _SERVER['PHP_SELF'] code from link it hides and display buttons as expected (but no php code run))
How can I retain the page update by Javascript and run the PHP code?
I'm new to PHP am I missing something in my code?
$_SERVER['PHP_SELF'] is only a refrence to the php document itself, it doesnt contain any key/value pairs. try using:
echo '<a href=" '.$_SERVER['PHP_SELF'].'?'. $_SERVER['QUERY_STRING'].
'&name=edit&id=' .$rowCountry->CountryId .
' " id="edit" onClick="MyFunction()"> Edit </a>';
edit:
i may have misunderstood the question. you may need something like this:
<?php
if(!empty($_GET[edit])){
//echo code that u want to show AFTER they click the edit link
}else{
//echo the code to show if they have NOT clicked the edit link
}
?>

Dynamic button always execute onclick event when made

So hello everyone I have a big problem right now with a button that is made depending on the variables that are sent to the page in this case it's a button the when it's clicked it will access a DB and extract everything in the DB and put it into excel format. The problem right now is when the button is made it always execute the DB search and file making. I know it has to do it because of the way the button is made right now, is there anyway of changing this?
if ($action_db) echo "<button class=\"btn\" onClick='".getUDB()."'>" . DBUser . "</button>";
to this:
if ($action_db) echo "<button class=\"btn\" onClick=\"getUDB()\">" . DBUser . "</button>";
and sending the event trough java-script so it executes the getUDB() method that's on my PHP file?
Thanks.
OK so I went and used part of the suggestion that Sam said and put it on a JQuery and the change is like this
if ($action_db) echo "<button class=\"btn\" id=\"UDB\">" . DBUser . "</button>";
JQuery:
<script type="text/javascript">
$(document).ready(function(){
$("#UDB").click(function(){
<?getUDB();?>
});
});
OK so the problem is that when I select the clients menu or any other part of the menu to access that part of the webpage it's automatically doing the getUDB method and won't let me access anything else. Any suggestions???
If you don't want the PHP function getUDB() to be called when you load the page, then you're right - you want to move this out into a Javascript function.
By doing echo "foo" . getUDB() . "bar", I'm assuming your page is loading slowly. To solve this, you can use Javascript with an AJAX call. So you probably want to:
Create a separate PHP file that calls getUDB().
Create a Javascript function on your original page that issues a GET request to this new, separate PHP file. It's easiest to do this using jQuery, which makes AJAX requests nice and easy - check out this page.
Write the Javascript needed to handle the response from that page appropriately.
SitePoint has a good tutorial on jQuery and AJAX, check it out.
You need to send a request to the server to initiate anything server-side in response to an event, either by a traditional post-back or via AJAX.
I suggest you make your buttons into forms, substituting the ID of the user for the value of the hidden input:
if ($action_db) {
?>
<form action="/getUDB">
<input type="hidden" name="DBUser" value="<?= DBUser ?>" />
<button class=\"btn\" type="submit"><?= DBUser ?></button>
</form>
<?
}
And then set up a second script to handle POST requests to /getUDB

How do you post data with a link

I have a database which holds the residents of each house in a certain street. I have a 'house view' php web page which can display an individual house and residents when given the house number using 'post'. I also have a 'street view' web page which gives a list of houses. What I want to know is if you can have links on the street view which will link to the house view and post the house number at the same time without setting up a form for each?
Regards
If you want to pass the data using POST instead of GET, you can do it using a combination of PHP and JavaScript, like this:
function formSubmit(house_number)
{
document.forms[0].house_number.value = house_number;
document.forms[0].submit();
}
Then in PHP you loop through the house-numbers, and create links to the JavaScript function, like this:
<form action="house.php" method="POST">
<input type="hidden" name="house_number" value="-1">
<?php
foreach ($houses as $id => name)
{
echo "$name\n";
}
?>
</form>
That way you just have one form whose hidden variable(s) get modified according to which link you click on. Then JavaScript submits the form.
I assume that each house is stored in its own table and has an 'id' field, e.g house id. So when you loop through the houses and display them, you could do something like this:
<a href="house.php?id=<?php echo $house_id;?>">
<?php echo $house_name;?>
</a>
Then in house.php, you would get the house id using $_GET['id'], validate it using is_numeric() and then display its info.
You cannot make POST HTTP Requests by some_script
Just open your house.php, find in it where you have $house = $_POST['houseVar'] and change it to:
isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']
And in the streeview.php make links like that:
Or something else. I just don't know your files and what inside it.
This is an old thread but just in case anyone does come across i think the most direct solution is to use CSS to make a traditional form look like an anchor-link.
#ben is correct you can use php and javascript to send a post with a link, but lets ask what the js does -- essentially it creates a form with style='display:none' sets an input/text line with value='something' and then submits it.
however you can skip all this by making a form. setting style='display:none' on the input/text lines (not the form itself as above) and then using CSS to make the button look like a normal link.
here is an example is i use:
in PHP Class,
public function styleButton($style,$text){
$html_str = "<form id='view_form' action='".$_SERVER['REQUEST_URI']."' method='post' >";
$html_str .= "<input style='display:none;' name='list_style' type='text' value='".$style."' >";
$html_str .= "<input id='view_button' type='submit' value='".$text."' >";
$html_str .= "</form>";
return $html_str;
}
Then in the CSS id="view_form" set "display:inline;"
and in the CSS id="view_button" set to something like: "background:none;border:none;color:#fff;cursor:pointer"
I would just use a value in the querystring to pass the required information to the next page.
We should make everything easier for everyone because you can simply combine JS to PHP
Combining PHP and JS is pretty easy.
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = $house_number; document.forms[0].submit();</script>";
Or a somewhat safer way
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = " . $house_number . "; document.forms[0].submit();</script>";
This post was helpful for my project hence I thought of sharing my experience as well.
The essential thing to note is that the POST request is possible only with a form.
I had a similar requirement as I was trying to render a page with ejs. I needed to render a navigation with a list of items that would essentially be hyperlinks and when user selects any one of them, the server responds with appropriate information.
so I basically created each of the navigation items as a form using a loop as follows:
<ul>
begin loop...
<li>
<form action="/" method="post">
<input type="hidden" name="country" value="India"/>
<button type="submit" name="button">India</button>
</form>
</li>
end loop.
</ul>
what it did is to create a form with hidden input with a value assigned same as the text on the button.
So the end user will see only text from the button and when clicked, will send a post request to the server.
Note that the value parameter of the input box and the Button text are exactly same and were values passed using ejs that I have not shown in this example above to keep the code simple.
here is a screen shot of the navigation...
enter image description here

Categories