I have researched as much as I can about this issue, and have so far not found a solution that works.
Essentially what I have is a PHP page where the user enters match results, the form action includes a header that takes them to the updated league table. I have disabled caching from the page where the user enters results, but this does not accommodate for the 'back' button being clicked in the browser. If they do click back, the page shows it's previous state, before the last scores they entered, which, if submitted, causes a real problem with the league tables.
At the minute I have got the form action to run in a new tab, meaning they cannot click 'back' in the browser, and instead must use a 'back' link, which means the page will always be re-rendered. Great, but the issue is that the form page is stil open in it's original tab.
Is there a way around this. I realise that calling a javascript closeWindow() function 'onSubmit' will close the window before the database is updated.
This is what I have so far, the new tab opens, but the original one remains open.
For added info, the page that I need to close, is opened in a new tab itself from a link on the homepage.
<?php
require_once "pdo_enl_connect.php";
$database=dbNB_connect();
echo "<table>";
echo "<form action=\"update_table_a.php\" target=\"_blank\" method=\"post\">";
$query=$database->query("SELECT team_id, team_name, team_score, opposition_score, opposition_id, opposition_name from results_a");
while ($row = $query->fetch(PDO::FETCH_NUM)) {
echo "<input type=\"hidden\" name=\"team[]\" value=\"$row[1]\">
<input type=\"hidden\" name=\"opposition[]\" value=\"$row[5]\">
<input type=\"hidden\" name=\"team_id[]\" value=\"$row[0]\">
<input type=\"hidden\" name=\"opposition_id[]\" value=\"$row[4]\">
<tr><td>$row[1]<td><input type=\"text\" name=\"team_score[]\" value=\"$row[2]\" <td>
<input type=\"text\" name=\"opposition_score[]\" value=\"$row[3]\"><td>$row[5] </tr>";
}
echo "</table>";
$query2=$database->query("SELECT team_score, opposition_score from results_a");
while ($row = $query2->fetch(PDO::FETCH_NUM)) {
echo "<input type=\"hidden\" name=\"prev_team_score[]\" value=\"$row[0]\">
<td><input type=\"hidden\" name=\"prev_o_score[]\" value=\"$row[1]\">";
}
echo "<input type=\"submit\" value=\"Go\" name=\"go\">";
echo "</form>";
if(isset($_POST['go'])) {
echo "<script>window.close();</script>";
}
?>
<?php
if(isset($_POST['buttonname']))
echo "<script>window.close();</script>";
?>
and Remove the window.close() from the onSubmit
Maybe try using AJAX for the onSubmit?
var request = new XMLHttpRequest();
request.open("POST","submitdata.php",false);
request.onload = function(){ window.close(); };
request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send("name="+name+"&surname="+surname+"&pass="+pass...);
This will send the form data to a php script that updates the data, then after the response loads, it will close the window. This is synchronous ("false" in the request opening), so users shouldn't be able to mess your database up.
Related
I'm looking for a suggestion on how to proceed. I currently have my application connected to an SQL database of tradesmen, and I have successfully been able to show search results on the same page within a table below the submit button. In this table, I have a small amount of information on each tradesmen returned, and at the end of the table row, there is a button with a value of 'more info'. When clicked, the button takes the user to another page titled 'info.php'.
I need to somehow have all of the information on that particular tradesmen generated on the info.php page when the user clicks that button. Since I need PHP for database queries and PHP doesn't seem to have onClick events like JavaScript does, I'm unsure how to make this happen. How do I link the button to the particular tradesman that it is associated with so I can return the correct results? How do I query the database in this case?
My code for my landing page is below. I don't need to be shown ALL of the code I would need to do this. I really just need to be pointed in the right direction. Thank you!
<body>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include("functions.php");
?>
<div class='nav'>
<ul>
<li><a href='#about' class='navLink'>About</a></li>
<li><a href='#login' class='navLink'>Login</a></li>
<li><a href='#contact' class='navLink'>Contact</a></li>
</ul>
</div>
<div class='wrapper'>
<h1>Welcome!</h1>
<div class='form_container'>
<form action='landing_page.php' method='post'>
<input type='text' name='trade' placeholder='What trade do you want to learn?'>
<p><button class='btn btn-success' name='submit' type='submit'>Find a mentor</button></p>
</form>
</div>
<?php
if(isset($_POST['submit'])) {
$trade = $_POST['trade'];
$results = query_database($trade);
if($results) {
$num_results = count($results);
print "<table><tr><th>First</th><th>Last</th><th>City</th><th>State</th><th>Available</th></tr>";
for($i=0; $i<$num_results; $i++) {
for($j=0; $j<5; $j++) {
if($j==0) {
print "<td>" . $results[$i][$j] . "</td>";
}
else if($j==4) {
print "<td>" . $results[$i][$j] . "</td><td><input type='button' value='more info' class='btnClass' onClick='location.href=\"info.php\"'></td></tr>";
}
else {
print "<td>" . $results[$i][$j] . "</td>";
}
}
}
print "</table>";
}
}
?>
</div>
</body>
When you click your "More Info" button at the end of each row of your grid, you can save the info of the current row salesman into hidden variables on your page which you can transmit to your "info.php" page through the GET or POST methods.
Append the salesman ID to the name of the button for each row to make it unique to each salesman. When you click the relevant row button, save the information you need for the current salesman row into the hidden variables and submit using GET or POST to "info.php" as a form submit.
Example:
If you want to transmit the salesman ID and Name, you can have 2 hidden variables on the page as below.
<input type="hidden" id="saleman_id">
<input type="hidden" id="saleman_name">
For each button at the end of each row for each salesman in the salesman search grid, you can have code like this:
<input type="button" id="<salesman_id>" value="more info" onclick="moreinfo.php?id=XXX&name=YYYY">
You have to have code in moreinfo.php to handle the variables being sent. For the above, the variables are sent using GET but you can also use the POST method if you do not want to show the info being sent.
I have a page filled with images pulled from a MySQL table. When one of these images is clicked, a modal pop-up window appears. This modal window contains backward and forward buttons, like this:
I would like to be able to iterate through the MySQL images with these buttons.
Here is the php code that grabs the images and displays them on the page:
<div id="screenings">
<?php
...
//connect to database & table
...
while ($row = mysqli_fetch_array($result)){?>
<div class='img_div' ng-click="vm.openModal('custom-modal-1')">
<img class='modal_img img_screenings' src='images/<?php echo $row['image']."' >";
echo "<p id='movie_p' align='center'>" .$row['title']."</p>";
echo "<p id='screenings_p' align='center'>" .$row['year']."</p>";
echo "<p id='location_p' align='center'>" .$row['duration']."</p>";
echo "</div>";
}
?>
</div>
Do I need to get $row['id'] and store it in a hidden variable, and use that to iterate backward and forward? Or is there maybe a next-child previous-child function I could tap into somewhere for this? jQuery would be a preferred solution since this page has some jQuery script at the bottom of it already, but I'm open to any ideas.
EDIT Or maybe I could store the image file paths in an array as I'm pulling them from the MySQL table? How could I do that?
I'm using php & mysql (with a pinch of javascript). This is a simple "edit" form, that auto-fills the various elements from data in a table. On the form is a set of radio buttons which are set by the data when the page loads. As in the "add new" form, this "edit' form has javascript functions which are triggered by onclick(), which display either another set of radio buttons or a text entry box, when the user makes a selection. (That part works just fine.)
How can I trigger/fire these scripts to display the appropriate elements automatically when the page loads? (Really don't want to delve into the jQuery arena at this time - as I need to get this piece of the project wrapped up ASAP.)
echo "<tr>";
echo "<td align=right>Transport: </td>";
echo "<td><input type=radio id=transppnd name=transp value='pnd'";
// $prepdel: var holding 1 or 0 from database table field
if($prepdel>0){
echo "checked";
}
echo" onclick='showpnd()'>Prep & Deliver";
echo " <input type=radio id=transpcpu name=transp value='cpu'";
// $prepdel: var holding 1 or 0 from database table field
if($prepdel<1){
echo "checked";
}
echo " onclick='showcpu()'>Customer pickup</td>";
echo "</tr>";
Looks like you want to call your showcpu() and showpnd() methods on page load. Just add a script like this at the bottom of your html page:
<script type="text/javascript">
showpnd();
showcpu();
</script>
</body>
I don't know php but maybe something like this?
if($prepdel>0){
echo "<script type="text/javascript">showpnd();showcpu();</script>";
}
*****************************UPDATE********************************
echo "<input type=hidden id=porc name=porc value='$prepdel'>";
<script type="text/javascript">
if(document.getElementById("porc").value > 0)
{
showpnd();
showcpu();
}
</script>
</body>
So I am learning PHP and mySQL, know HTML and JS. I am making a small note-taking webapp for my self, and stumbled upon a problem. I am generating divs by using a foreach and echo. The notes are stored in a db and requested using "SELECT"-mysql. The delete-button (works as a button but is text) is connected to a form which is also generated; One form for every note.
echo "<form method='post' id='" . $row['id'] . "form" . "' action='handler.php'>";
echo "<input type='hidden' name='deleteRecord_button'";
echo "<input type='hidden' name='" . $row['id'] ."'>";
echo "</form>";
When I submit the form using the delete-button a form is send as POST to a handler.php-file, however I can't seem to find a good way of sending the "id" of the note so that I can
delete it from the database using mysqli "DELETE FROM notes WHERE id = THE ID OF THE NOTE...
Sorry if this makes no sense, but I want to know if there is a good solution as to how I can send the id from notes.php (the picture below) to the handler.php file where I can handle and delete the note from the database. I prefer using "plain" JS, PHP, but I am using jQuery as well.
Change this line ...
echo "<input type='hidden' name='" . $row['id'] ."'>";
to
echo "<input type='hidden' name='id' value='" . $row['id'] ."'>";
Since you're making your form field names contain the ID, you'd need something like this:
$formIDs = preg_grep('/^\d+form$/', $_POST);
foreach ($formIDs as $id_string) {
$id = substr($id_string, 0, -4);
... do something with the ID ...
}
e.g..
<input type="hidden" name="42form" ?>
producing
$formIDs = array('42form');
You didn't specify the value of the 'deleteRecord_button' input. If you add it like I did, it'll work.
<form method="POST" id="<?php echo $row['id'] ?>form" action="handler.php">
<input type="hidden" name="deleteRecord_button" value="<?php $row['id']; ?>"/>
</form>
.
Now, a tip you can ignore if you want.
Instead of using a form to post a value to another page you can also use the GET method. This can be done with less code.
Delete
If you catch the GET in handler.php and if necessary check if there is a user logged in and then execute the database command. If you do, make sure if the delete variable is a number. You could use is_numeric for that.
Basically, I'm trying to create a login system, and I'm using it what I call "Dynamically" meaning it's included from my other files, and if I wanted to use a different database I would simply pass that database to the login function. I know how to do this by default, but as soon as using a button came in I got a little confused.
Here's what I have in it's most basic form.
<?php
createLogin('test', 'test2');
function createLogin($SQLConnection, $SQLConfig) {
echo "<h1> You are currently not logged in!</h1>";
echo "<form action='handleLogin(".$SQLConnection.",".$SQLConfig.") method='post'>";
echo "<div align='center'>";
echo "<table style='width: 475px'>";
echo "<thead>";
echo "<th>";
echo "<tr>Enter your e-mail and password.</tr>";
echo "</th>";
echo "</thead>";
echo "</table>";
echo "<input type='submit' value='Login' />";
echo "</form>";
}
function handleLogin($foo, $bar) {
echo $foo . " || " . $bar;
}
?>
When I click the submit button however, it simply takes me here...
http://localhost/handleLogin%28test,test2%29%20method=
Now, I read about using Javascript to do this, and to do something like
<script>
function processLoginRequest($SQLConnection, $SQLConfig) {
alert("<?php handleLogin($SQLConnection, $SQLConfig) ?>");
}
</script>
Then I could use
echo "<form action='processLoginRequest(".$SQLConnection.",".$SQLConfig.") method='post'>";
However, the code causes the entire php script to die. (Without error?)
You're using action incorrectly, and the result is as expected. action stores the page to which the form will be submitted. So, yes, when you hit submit it is trying to take you to a page called handleLogin%28test,test2%29%20method= because that is what your action says to do.
What you can do is simply leave the action blank, which will submit the form to the current page. Then, on that page, check if the form has been submitted, and if so, call your function.
Inside the function that creates the form make these changes:
function createLogin() {
...
echo "<form action='' method='post'>";
....
echo "<input type='submit' value='Login' name='login'/>";
}
Then, at the top of the page that renders the form, add something like this:
// Check if login form has been submitted - if so, handle
if (isset($_POST['login'])) {
handleLogin($SQLConnection, $SQLConfig);
}
// Render login form. No need to pass config parameters here.
createLogin();
If you really want to keep everything in a single function, I suppose you could also do it like this:
function createLogin($SQLConnection, $SQLConfig) {
if (isset($_POST['login'])) {
handleLogin($SQLConnection, $SQLConfig);
}
else {
echo "<h1> You are currently not logged in!</h1>";
echo "<form action='' method='post'>";
echo "<div align='center'>";
echo "<table style='width: 475px'>";
echo "<thead>";
echo "<th>";
echo "<tr>Enter your e-mail and password.</tr>";
echo "</th>";
echo "</thead>";
echo "</table>";
echo "<input type='submit' value='Login' name='login'/>";
echo "</form>";
}
}
You do NOT want to pass your SQL Configuration parameters back to JavaScript, because anyone can look at your JavaScript code when they browse your page, and then they'll have everything they need to connect and play around in your database.
You will have to pass some kind of flag to your form, to let your PHP code know (when it receives the form's data later) what kind of SQL settings it should use.
Example:
<form method="POST" ...>
<type input="hidden" name="loginMode" value="<?php echo $loginMode; ?>" />
</form>
Again, don't pass any sensitive data in there, just have some kind of unique value like "mySql" for $loginMode or the other options.
And then, when you're handling the HTTP POST in your PHP:
if ($_POST['loginMode'] == 'mySql')
{
// ... create connection based on $SQLConnection, $SQLConfig
}
else if ($_POST['loginMode'] == 'otherMethod') ...
Your JavaScript is probably failing because of the the contents of $SQLConnection and $SQLConfig. If you have a double quote in them it would fail.
Also designing and implementing a safe and robust login system is actually pretty difficult and you should opt using a framework that has been tested over time.