using the export button function to another php file - php

This is the form.php file and i would like to put export button file to another php file. I am still rocky for php language.
form php
<form action="<?php echo url_for("attendance/viewAttendanceRecord"); ?>" id="reportForm" method="post" name="frmAttendanceReport">
<fieldset>
<ol>
<?php
if ($form->hasErrors()) {
echo $form['employeeName']->renderError();
}
?>
<?php echo $form->render(); ?>
<?php echo $form->renderHiddenFields(); ?>
<li class="required">
<em>*</em> <?php echo __(CommonMessages::REQUIRED_FIELD); ?>
</li>
</ol>
<p class="formbuttons">
<input type="button" class="" id="btExport" onclick='myfunction()' value="<?php echo __('Export') ?>"/>
I want to use export function in this action.php file
action.php
if($post['export'] == '1')
{
//statement
}

Php uses name attribute of the elements in order to build $_POST array. So add name attribute to button along with other input elements as follows.
<input type="submit" id="btExport" name="btExport" value="<?php echo __('Export') ?>"/>
in action.php do something like the following to handle posted values.
if($_POST["btExport"])
{
//statement
}
or you can also check if $_POST is empty or not as follows
if (!empty($_POST))
{
//statement
}

Related

Deleting divs via PHP

I'm making a To do application with PHP (school assignment)
At this moment, I can add tasks to the list. Deleting is the next problem.
The "problem" is that I HAVE to use PHP to delete the corresponding div. (It needs to delete the div i'm clicking)
My question: what's the best practice to do that? (Working with a specific number maybe?)
Index.php
<div class="container">
<form action="/Periodeopdracht/index.php" method="POST">
<div class="headerToDo">
<input class="addText title" type="text" value="Click to add a task" name="nextToDo">
<input class="clickablePlus" type="submit" value="+" name="submit"></div>
</form>
<?php if(!$empty): ?>
<?php foreach ($_SESSION["todoList"] as $_SESSION["key"] => $toDo): ?>
<div class="toDo">
<form action="/Periodeopdracht/index.php" method="POST">
<button value="<?php echo $_SESSION["key"] ?>" name="done" class="done" type="submit" >V</button>
<div value="<?php echo $_SESSION["key"] ?>" class="textToDo"><?= $toDo ?></div>
</form>
</div>
<?php endforeach ?>
<?php endif ?>
</div>
application.php:
<?php
session_start();
$GLOBALS["empty"] = true;
$_SESSION['todoList'] = isset($_SESSION['todoList']) ? $_SESSION['todoList'] : array();
if(isset($_POST["submit"]))
{
$empty = false;
array_unshift($_SESSION['todoList'], $_POST["nextToDo"]);
}
if (isset($_POST['done'])) {
foreach ($_SESSION["todoList"] as $key => $toDo) {
if ($toDo == $_POST['done']) {
unset($_SESSION['todoList'][$key]);
break;
}
}
}
?>
foreach ($_SESSION["todoList"] as $key => $toDo)
then use $key as the value of your "done" button, When processing you can just unset($_SESSION['todoList'][$key])
you will have to check that the key is valid coming from the post though.
Add a <hidden> field to each toDo with, the id of the toDo, then you will know what to remove from the toDo list.
In addition to what exussum was stating. U'll need to define in your HTML which todo item u want to delete. Atm your just posting an empty button. Change your html to something like this:
<div class="toDo">
<form action="/../index.php" method="POST">
<button name="done" class="done" value="<?= $toDO ?>" type="submit">V</button>
<div class="textToDo"><?= $toDo ?></div>
</form>
</div>
Now if you post the form the variable $_POST['done'] will contain the task that is completed. Now the check this :
<?php
if (isset($_POST['done']) {
foreach ($_SESSION["todoList"] as $key => $toDo) {
if ($toDo == $_POST['done']) {
unset($_SESSION['todoList'][$key]);
break; //terminates the loop as we found the correct item
}
}
$empty = empty($_SESSION["todoList"]);
}
I'm not a huge fan of posting raw values to check whether items need to be deleted.
Its beter to work with (unique) id's if you are using these.

$_GET not working properly

i have a code in which i received two variable($isbn,$eno) from former page via form GET method but these two variables are not working if i am not echo out it on my page the code for the same is given below.
<?php
error_reporting(E_ALL);
require 'db/connect.php';
if(isset($_POST['generatereport']))
{
$isbn=$_GET['isbn'];
$eno=$_GET['eno'];
echo $eno; //if this is not done then i am not receiving data from database
echo $isbn; //if this is not done then i am not receiving data from database
$studentdata="select * from users where eno='$eno'";
if($studentresult=$db->query($studentdata))
{
$studentrow = $studentresult->fetch_assoc();
}
else
{
echo"fetching error";
}
$bookdata="select Lpad(isbn,'10','0') as isbn,book_name from book_data where isbn='$isbn'";
if($bookresult=$db->query($bookdata))
{
$bookrow = $bookresult->fetch_assoc();
}
else
{
echo"fetching error";
}
}
?>
<!doctype html>
<html lang='en'>
<head>
</head>
<body>
<div id='report'>
<table>
<tr><td><h3>Issue Report</h3></td></tr>
<tr><td><h4>Student details</h4></td></tr>
<tr><td>UNIQUE ID:<?php //random number here ?></td></tr>
<tr><td>Enrollment:<?php echo $eno; ?></td></tr>
<tr><td>Name:<?php echo strtoupper($studentrow['fname']);echo strtoupper( $studentrow['lname']); ?></td></tr>
<tr><td>Branch:<?php echo strtoupper($studentrow['branch']); ?></td></tr>
<tr><td>Semester:<?php echo $studentrow['sem']; ?></td></tr>
</table>
<hr/>
<table>
<tr><td><h4>Book details</h4></td></tr>
<tr><td>isbn:<?php echo $bookrow['isbn']; ?></td></tr>
<tr><td>Book Name:<?php echo strtoupper($bookrow['book_name']);?></td></tr>
</table>
<hr/>
<form action="script/issue.php?isbn=<?php echo $isbn;?>" method='post' id='report'>
<input id="btn_issue" type="button" value="Issue this Book"/>
<input id="btn_close" type="button" value="cancel"/>
</form>
</div>
</body>
</html>
You need to set attribute name to inputs in your form, after that you can access to value by GET or POST
Change form method:
<form action="script/issue.php?isbn=<?php echo $isbn;?>" method='get' id='report'>
<input name ='isbn' id="btn_issue" type="button" value="Issue this Book"/>
<input name ='eno' id="btn_close" type="button" value="cancel"/>
</form>
Or get your variables from post, like:
$isbn=$_POST['isbn'];
$eno=$_POST['eno'];
change your from method to get
<form action="script/issue.php?isbn=<?php echo $isbn;?>" method='get' id='report'>
or you can use $_REQUEST in php which can read either get or post
If you use method="POST", it meant you should use $_POST for your next process. Can use $_REQUEST also. But I think $_POST is more specifics for method POST. Please read the documentation of PHP about form method again.

In nested if statement form's if statement not work

if($media=="pet")
{
if($pressure=="bar")
{
if($f_req=="ltr/m")
{
$kvreq=$flowreq/16.666666667*16.6667/(pow(($presur*1/0.98066/660*1000),0.5));
echo "<b>KV Required:- </b>$kvreq ";
?>
<br/><br/>
<?php
if($kvreq > 14 and $kvreq <= 38){
$minOrfice=10;
$n_kv=38;
$maxOrfice=7;
echo "<b>Minimum Orfice Required:- </b>$minOrfice";?>
</br></br>
<?php
$n_kv1=($n_kv/16.66667*(pow($presur*1/0.98066/660*1000,0.5)))*16.666666667;
echo "$n_kv1 <b>liter/min</b>";?>
</br></br>
<?php
echo "The Max Flow at $maxOrfice orfice";?>
</br></br>
<?php
$maxfo=((14/16.66667)*(pow(($presur*1/0.98066/660)*1000,0.5)))*16.666666667;
echo "$maxfo <b>liter/min</b>";?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input name="chkb1" type="checkbox" />
<input type="submit" value="Submit" name="chk_btn" id="chk_btn"/>
</form>
<?php
if(isset($_POST['chk_btn'])){
echo "$abc";
}
}
}
}
}
...................................................................................................................................................................................
your form method is GET while you're attemptig to get POST data
change this:
if(isset($_GET['chk_btn']))
if(isset($_POST['chk_btn'])) to if(isset($_GET['chk_btn'])) should fix the problem but OP refuse that still cannot display any text.
I suspect that upon submitting the the form again in the page it lose the value of $media, $pressure and so on there it never pass the if statements and never proceed on next process
if($media=="pet")
{
if($pressure=="bar")
{
if($f_req=="ltr/m")
{

Add list item every time i click submit

I want a script which will echo a text from a form to a div tag every time i click the submit button.
i was able to do that in no time but i want the text to still be displayed in the div even when i submit another. i want every new submitted text to create a list. adding it to a previous list.
may be this got to do with database but i will like to know as every time i click the submit i only get the current text been submitted.
example of such script
<?php
$text = $_POST['text'];
?>
<html>
<div>
<?php echo "<ul>";
echo "<li>".$text."</li>";
echo "</ul>";
?>
</div>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="text" /><br/>
<input type="submit" value="Submit"/>
</form>
</html>
i want to just be adding entries to the <li> list every time i click submit.
I'm happy you're having fun. Here's a quick "starter for 10" :)
<?php
$items = array();
if('POST' === $_SERVER['REQUEST_METHOD']) {
if( ! empty($_POST['item'])) {
$items[] = $_POST['item'];
}
if(isset($_POST['items']) && is_array($_POST['items'])) {
foreach($_POST['items'] as $item) {
$items[] = $item;
}
}
}
?>
<html>
<head>
<title>Demo</title>
</head>
<body>
<?php if($items): ?>
<ul>
<?php foreach($items as $item): ?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<form method="post">
<input type="text" name="item" />
<input type="submit" value="Add Item" />
<?php if($items): ?>
<?php foreach($items as $item): ?>
<input type="hidden" name="items[]" value="<?php echo $item; ?>" />
<?php endforeach; ?>
<?php endif; ?>
</form>
</body>
</html>
You could use sessions to handle this if the list is temporary:
<?php
session_start();
if(isset($_POST['text']) && trim($_POST['text']) != "")
{
// add to a session array
$_SESSION['text'][] = $_POST['text'];
}
?>
<html>
<div>
<ul>
<?php if(isset($_SESSION['text']) && !empty($_SESSION['text'])): foreach($_SESSION['text'] AS $text): ?>
<li><?php echo $text; ?></li>
<?php endforeach; endif; ?>
</ul>
?>
<!-- rest of your html here -->
thought I would chime in too. This is a simple solution that will work only in the current instance of a page.
<?php
if ( isset( $_POST['text'] ) ) { # Find out if the form had been submitted
$text = $_POST['text']; # If so then store the submitted text in the $text var
if ( isset( $_POST['previous'] ) ) { # Find out if there were any previous inputs
$current = $_POST['previous'] . "," . $_POST['text']; # If there were then pop the latest one on the end of the previous ones with a comma and make that our current set of text
} else {
$current = $_POST['text']; # Otherwise the current set of text just comprises our most recently input text
}
}
?>
<html>
<div>
<?php
if ( isset( $_POST['text'] ) ) { # Find out if some text was input
$text_arr = explode(",", $current); # If it was then take our current set of text and make an array from it
echo "<ul>"; # Start the list
foreach ( $text_arr as $text ) { # For each item of text that has previously been input
echo "<li>".$text."</li>"; # Print out the list item with the text in it
}
echo "</ul>"; # End our list
}
?>
</div>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?php if ( isset( $_POST['text'] ) ) { ?> # If the previous form submitted some text
<input type="hidden" name="previous" value="<?php echo $current ?>" /> # Store it in a hidden input field to be submitted later
<?php } ?>
Name: <input type="text" name="text" /><br/>
<input type="submit" value="Submit" />
</form>
</html>
So this will do what you want but without any storing into a database. If storing into a database is what you want to do then you might want to do some research into MySQL or some other method of permanently storing list items.
Hope mine has been of some help, I'm sure many others have popped an answer on while I have been typing this...
You would need to keep adding to a session variable and best to use an array.
Like so;
<?php
session_start(); // This is needed to keep your session
if(!isset($_SESSION['text'])){
// set session array if not already set previously
$_SESSION['text'] = array();
}
if($_SERVER['REQUEST_METHOD'] == 'POST' && strlen($_POST['text']) > 0){
// add text to session array and escape for security
$_SESSION['text'][] = htmlspecialchars($_POST['text'], ENT_QUOTES);
}
?>
<html>
<div>
<ul>
<?php foreach($_SESSION['text'] AS $text): ?>
<li><?php echo $text; ?></li>
<?php endforeach; ?>
</ul>
</div>
<form method="POST">
Name: <input type="text" name="text" /><br/>
<input type="submit" value="Submit"/>
</form>
</html>
Edit: This only works for the current session, if you want to come back later and see the same list. You would need to store the values somewhere like a database.

Change the value of 1 input into the value of another input on click

another JQuery question, I wanted to change the value of the hidden input based on the value of another hidden input. I'm having some trouble with this already. here's my code so far. This is not working but I think you can already get the idea of what I'm trying to do with this:
HTML code:
<div>
<div id="search_result_fake_container">
<div id="search_result_fake_div2"></div>
<form method="GET" id="getattendees">
<input type="text" id="search_result_fake2" value="<?php if(!empty($event_selected)){echo $event_selected;} else{echo "Select Event";}?>" name="event_name">
<input type="hidden" id="search_result_fake2_id" value="<?php if(!empty($event_selected)){echo $event_selected;} else{echo "0";}?>" name="event_id">
</form>
</div>
<div id="search_result_present_list2">
<?php foreach ($events as $events1): ?>
<div class="search_result_list_item2" id="search_result_item_12" style="text-align:center;"><?php echo $events1['event_name']; ?></div>
<input type="hidden" id="event_id" value="<?php echo $events1['event_id']; ?>">
<?php endforeach ?>
</div>
</div>
and the JQuery:
$("#search_result_fake_div2").live("click", function () {
$("#search_result_present_list2").show("fast");
$('body').one('click',function() {
$("#search_result_present_list2").hide();
});
event.stopPropagation();
});
$(".search_result_list_item2").live("click", function () {
$("#search_result_fake2").val($(this).html());
$("#search_result_fake2_id").val($("#event_id").val());
$("#getattendees").trigger('submit');
$("#search_result_present_list2").hide();
});
I use GET just to check if the values are moving and I'm not that sure if the line $("#search_result_fake2_id").val($("#event_id").val()); is really working.
Add the event parameter to your #search_result_fake_div2 click handler, like so:
("#search_result_fake_div2").live("click", function (event) {
^--------HERE
Because otherwise you will have a javascript runtime error when you try to use it later in:
event.stopPropagation();

Categories