Refreshing a php page after submit button - PHP - MySQL - php

I have a page that needs to be constantly refreshed multiple times a minute. The page has is a echo'd php table.
The page loads perfectly fine, all is good, I have used the META tag HTML, I have used the header tag with the refresh function in PHP... and yet a problem arises :
When I hit Start Session button the refresh stops. And the table bellow does does not get updated. So then I have to manually refresh the page. This is not the desired affect. Can some one explain to me how to refresh a page continually.
Edit 1:
Code that makes the include of the session start
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> <a href=student.php?anum=" . $row['anum'] . " target='_blank'>" .$row['anum'] . " </a></td>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['why'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "<td>" . $row['additional_req'] . "</td>";
echo "<td>" . $row['signintime'] . "</td>";
echo "
<td> <form action='counselor.php?id=" . $row['id'] . "' method='post' target='_new'>
<select name='namedrop'>
<option value=''>Counselor Name</option>
<option value='Admin-John'>Admin - John</option>
<option value='Admin-Christine'>Admin - Christine</option>
<option value='Admin-Dawne'>Admin - Dawne</option>
<option value='Counselor-Cherie'>Counselor - Cherie</option>
<option value='Counselor-Tootie'>Counselor - Tootie</option>
<option value='Counselor-Debbie'>Counselor - Debbi</option>
<option value='FrontDesk-Delores'>Front Desk - Delores</option>
<option value='FrontDesk-Kiana'>Front Desk - Kiana</option>
</select>
</td>
<td> <input type='submit' name='submit' value='Start Session'></td>
</form> </td>";
}

If you are looking to refresh the page when you click "Start Session", then you can edit your submit button to have this onClick listener. It should refresh the page.
<input type='submit' name='submit' value='Start Session' onClick="window.location.reload()" />

Is it possible for you to use Javascript to achieve the desired effect? I don't know enough about what you're doing to determine whether that would clear your changes or not (my assumption here is: no).
If you are able to use Javascript, you could write a simple function which refreshes the page on an interval.

<form action="<?php echo $PHP_SELF; ?>" method="post">
or
<form action="thispage.php" method="post">

Related

Display user defined number of MySql results with dynamicly updating content [php]

I'm trying to limit the number of rows that the page shows according to the users selection from a drop down(think like a store page "show number of items per page")
At the moment I'm using php to call MySql and then echo out my results. I know that php cant do anything after the page loads. The next thing that comes to mind is java script.
However I have no experience with java script, I'm fair normal with java.
What are other options that you would suggest?
note: I want to limit my while loop not the mysql result.
Here is my code as it stands now:
<form name="input" action="EditPartyP.php" method="post">
<tr>
<td>Party ID</td><td>Party Name</td>
<td>Start Date</td><td>End Date</td><td>Sales</td><td>VIEW:
The drop down selection options that are being offered.
<select>
<option value="10">10</option>
<option value="20">20</option>
<option value="40">40</option>
<option value="80">80</option>
</select>
</td>
The results from the query being displayed, I under stand that I will have to change this because the php is done when the user sees the output html.
<?php
$ID = $_SESSION['ID'];
$result = PartyData::PartyLookupByID($_SESSION['ID'], "DESC");
This loop now just runs until the result runs out of data.
I need to make it stop when the users number is reached as well.
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<form name="input" action="EditPartyP.php" method="post">';
echo "<td>" . $row['PartyID'] . "</td>";
echo "<td>" . $row['PartyName'] . "</td>";
echo "<td>" . $row['sDate'] . "</td>";
echo "<td>" . $row['eDate'] . "</td>";
echo "<td>" . $row['PartyOrderTotal'] . " </td>";
echo '<input type="hidden" value=" ' . $row['PartyID'] . '" name="PartyID">';
echo "<td>" . '<button type="submit" value="Edit" name="Action">Edit</button>' . "</td>";
echo '</form>';
echo "</tr>";
}
?>
</table>
Thank you for reading and a comment helps more than a down vote.
You might want to look into https://datatables.net if you want features such as per page row diaplay or so called pagination.
Based on the selected page (via a $_GET['var'] and the users choice of number of records per page, you have to adjust your query with the limit statement.
SELECT * FROM table LIMIT 10, 50
Would start at the 10th record and will select 50 records max.
You can use SQL Limit command to retrieve specific number of data.
SELECT column_name(s) FROM table_name LIMIT number;

Fetch db data depending on selected drop down value

Is it possible to fetch data from db upon changing value of my drop down, I want to pull up the data that corresponds to the value of the drop down and show it to a text area, I got the codes that I've tried to create just don't know how to make it work to what I want.
I want my page to just initially show the drop down then after selecting a value it will show the data in a text area without refreshing the page, here is the code:
<div id="mainContent">
<table width="619" border="0" align="center">
<td align="center"><form id="form1" name="form1" method="post" action="" >
<fieldset>
<legend><strong>EA</strong></legend>
<p>
<select name="ea_name" id="ea_name">
<option value="" selected="selected">Please select...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</p>
</fieldset>
</form></td>
</table>
<div id="results"></div>
</div>
I am thinking an "onchange" could do it but just dont know how to implement it, also want to echo the resulting data to a text area within a fieldset.
Here is the code that I've tried that will pull up data and show to a text area:
<?php
require 'include/DB_Open.php';
$ea_name = $_POST['ea_name'];
$sql="SELECT * FROM ea_error WHERE ea_name = '" . $ea_name . "'";
$myData = mysql_query($sql);
//to count if there are any results
$numrow = mysql_num_rows($myData);
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo "<fieldset><legend><strong>Information</strong></legend><p>
<table width="auto" height="172" border="0">
<tr><th scope="row">Error</th></tr>
<tr><th scope="row">Resolution</th></tr>
<tr><th scope="row">Contact/s</th></tr>;"
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['error'] . "<input type=hidden name=error value=" . $info['error'] . " </td>";
echo "<td align='center'>" . $info['resolution'] . "<input type=hidden name=resolution value=" . $info['resolution'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['contacts'] . "<input type=hidden name=contacts value=" . $info['contacts'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</fieldset>";
include 'include/DB_Close.php';
?>
UPDATED CODE:
<?php
require 'include/DB_Open.php';
$ea_name = $_POST['ea_name'];
$sql="SELECT * FROM ea_error WHERE ea_name = '" . $ea_name . "'";
$myData = mysql_query($sql);
//to count if there are any results
$numrow = mysql_num_rows($myData);
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo '<fieldset><legend><strong>Information</strong></legend><p>
<table width="auto" height="172" border="0">
<tr><th>Error</th></tr>
<tr><th>Resolution</th></tr>
<tr><th>Contact/s</th></tr>';
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['error'] . "<input type=hidden name=error value=" . $info['error'] . " </td>";
echo "<td align='center'>" . $info['resolution'] . "<input type=hidden name=resolution value=" . $info['resolution'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['contacts'] . "<input type=hidden name=contacts value=" . $info['contacts'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</fieldset>";
include 'include/DB_Close.php';
?>
This should do the trick. Just put this javascript either in script tags in the head of your page, after the jQuery include, or in another js file and include that after jQuery...
$(function() { // document.ready
$("#ea_name").on("change", function() {
$.ajax({
url: "phpfile.php",
type: "POST",
data: {
ea_name: $(this).val()
},
success: function(data) {
$("#results").html(data);
}
});
});
});
It assigns an event handler to the change event of the drop down. When this is triggered it sends an ajax request to your php file (don't forget to put the correct filename for the url!) which then returns the html. This is then pushed into the results div.
Note: I fixed a typo that may or may not be in your php file. At the end of the line where you create the query, you'd missed a closing ". Just in case that was a copy and paste from the real file.
You have to implement ajax call to php file with ea_name as post parameter file. and place response to specific div.

Delete button for each table row

I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.

Pull information from an array

So basically I don't really know how to explain what i need or if the title is even correct but i need some help.I'll explain what I'm trying to do the best I can.
This is a picture of the page I'm working on and referring to
http://i40.tinypic.com/14m5euh.png .
SO basically what I need to do is,when the user uploads a file and clicks on upload it will upload to a database along with the job number,so far I've managed to get it to update into the database.
But I can't get the job number in the database because my information is all in an array from the database I tried to store the information into a session variable,its loops and gets replaced by the next one,so basically what ever the last job number is will be stored in the database which of cause is wrong?
The last problem I've is,how do I get the users answer from the drop down box into a variable ?
My code:
$con = mysqli_connect("localhost", "root", "","fixandrun") or die(mysqli_error());
$data = mysqli_query($con,"SELECT * FROM bookjob") or die(mysql_error());
Print "<table border cellpadding=10>";
Print "<table border='2'>";
echo "<table border='2' cellpadding=10>
<tr>
<th> Job number</th>
<th> Job details</th>
<th> Pc number </th>
<th> Job status</th>
<th> Upload report</th>
<th> Update Job</th>
</tr>";
while($row= mysqli_fetch_array($data))
{
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td> <form action='reportupload.php' method='post' enctype='multipart/form-data' name='uploadform'>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
<input name='upload' type='submit' id='upload' value='Upload'>
</form>
</td>";
echo "<td> <a href='updateadmin.php'>Update information</a></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
There are many problems with your code. It would be extremely time consuming to go through them all, so I'll run down some things you need to look at to get this going.
FIrstly, you can make as many forms as you want, and with the help of PHP you can do this dynamically as youre doing. The problem is for every job, youre creating a new form with the same exact name. You need to find a way to make them the name of each form dynamic if youre going to do that.
Secondly, you probably shouldnt make a new form, so take all your opening and closing tags out of the loop, put the opening form tag before the while, and the ending form tag after the while loop.
I would suggest you go read about HTML forms a little more and PHP while loops and fetching information from a database with embedded forms before messing with this more. No offence, but I think you need a little more knowledge on this before fiddling with it.
I would suggest making a test database and table with maybe 3 fields, then making a small form with a couple field and seeing if you can fetch the data and update it with your form first.
i think i have done what you ment, is this right now?
$cnt = 0;
while($row= mysqli_fetch_array($data))
{
echo " <form action='test.php' method='post' enctype='multipart/form-data' name='uploadform' . $cnt>";
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'. $cnt] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
</td>";
echo "<td><input name='upload' type='submit' id='upload' value='Upload'></td>";
echo "</tr>";
echo "</form>";
++$cnt;
}
echo "</table>";
echo "<br>";
?>

radio modification with php

I have this script =>
while ($data = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td><span class='getinfo'>" . $data['username'] . "</span></td>";
echo "<td></td>";
echo "<td><span class='getinfo'>" . $data['email'] . "</span></td>";
echo "<td></td>";
echo "<td><span class='getinfo'>" . $data['rights'] . "</span></td>";
echo "<td></td>";
echo "<td><span class='getinfo'>" . $data['last_seen'] . "</span></td>";
echo "<td></td>";
echo "<td><span class='getinfo'>" . $data['since'] . "</span></td>";
echo "<td></td>";
echo "<td><span class='radio_place'><input type='radio' name='choose' onclick='showModification()' id='get_" . $data['username'] . "' value='" . $data['rights'] . "'></span></td>";
echo "</tr>";
}
And like you see guys, I have radio buttons and after onclick them I have made to show up next form =>
and with showModification() function (javascript) I appear next html form for data modification
and here is the face of this form =>
<div id="mod_div">
<select name="option" id="option">
<option value="sel">* * * *</option>
<option value="null">0</option>
<option value="one">1</option>
<option value="del">Delete</option>
</select>
<input type="submit" name="action" id="action" value="Action">
</div>
I want to know after pressing action submit which radio is clicked to manipulate it with php, please help me how to do that ? Thanks ...
If it is a new page, you can use $_GET or if it is in same page you can use JavaScript itself to push which radio button is clicked. Example as follows.
<div id="mod_div">
<select name="option" id="option">
<option value="sel">* * * *</option>
<option value="null">0</option>
<option value="one">1</option>
<option value="del">Delete</option>
</select>
<input type="submit" name="action" id="action" value="Action">
<input type='hidden' name='cameFrom' />
</div>
In PHP:
$_POST["choose"] or $_GET["choose"]
will give you an access to the clicked radio button. (the usage of _POST or _GET depends on your "action" attribute of your "form" tag.

Categories