How do I check with PHP if this code has been clicked so I can use it in an if statement?
<form id="rating" action="index.php" method="post">
Rating
</form>
So if it is clicked I want to use this query:
if () {
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}
You would have to add an input, like <input type="hidden" name="hidden_element" value="data"/> to your form, otherwise there is no POST data for the server to receive.
Then in the index.php script you can check if $_POST['hidden_element'] is set.
For your example:
if (isset($_POST['hidden_element']) {
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}
So since your current form doesn't submit any data i like the technique of if multiple buttons are inside a form each should have its propper name and a type of submit
and on php you check like
if (this button was pressed then)
else if (this button was pressed then)
else (redirect in none or what ever you need to do when landed)
your form, i changed the ahref to an input type submit with the name of button
<form id="rating" action="index.php" method="post">
<input type="submit" name="button"
onclick="document.getElementById('rating').submit();">Rating
</form>
the php action should look like this, you can later implement ajax calling here
if (isset($_POST['button']) === true && empty($_POST['button']) === tre)
{
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}
Related
I want to make an invoice, the clients are shown in a table with the foreach loop. The data is extracted from my SQL database with PDO like this:
include 'C:\xampp\htdocs\App\include\dbconnect.php';
$sql = 'SELECT * FROM clienten WHERE verzekeraar = ?';
$stmt = $database->prepare($sql);
$stmt->execute(array($_SESSION["ROL"]));
$clienten = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($clienten as $client) {
But now I have made a button in the table, and when it is pressed you get redirected to make_invoice.php and the data is converted to PDF to be printed / e-mailed etc. based on the client_ID that is in the table like this:
Polisnummer:
How do I get the $client['clientID'] to make_invoice.php when this button is clicked?
<tr><th>Create invoice </th><td><form action='make_invoice.php' method="POST">
<input type="submit" name="aanmaken" value="Click here">
</form>
</td></tr>
I have fixed this by adding
<input type="hidden" value="$client['clientID']">
to the form.
I have a dynamic menu inside a form element code is here
<form method="GET" action="index.php">
<?php
display_menu(); // this function generates menu items
?>
</form>
after the menu is generated every menu item is a submit button of the above form I want to get input of a single element by name or id attribute of submit button, and load a post from database.
<form method="GET" action="index.php">
<input type="submit" name="page-1" id="page-1" value="page-1">
<input type="submit" name="page-2" id="page-2" value="page-2">
<input type="submit" name="page-3" id="page-3" value="page-3">
<input type="submit" name="page-4" id="page-4" value="page-4">
</form>
so when any input button is pressed the function display_post() is called. Code of the function is as follows:-
function display_post(){
$conn = mysqli_connect('localhost', 'root', '', 'posts') or die('cannot connect');
if($_SERVER['REQUEST_METHOD'] == 'GET' ){
$blog_post_id = $_GET["id"];
$sql = "SELECT * FROM blog_posts where id='blog_post_id' ";
$result = mysqli_query($conn, $sql) or die('cannot load');
while ($row = mysqli_fetch_assoc($result)){
if($row > 0){
echo '<div>'.$row['content'].'</div>';
}else echo 'no posts';
}
}
}
However, the display_post() method is called inside a content tag whereas the display_menu() is called inside another div.
So the problem is I'm unable to get the id of the to submit button any help will be appreciated thanks in advance.
When you click on the submit button you will get all inputs values of the form.
Do one thing put this statement print_r($_GET);exit; in display_post() as a first line and see what you get after clicking on submit button.
Note: 1. you want to get the value of clicked button then you should use javascript or jQuery and Ajax.
2. You can not get input fields value by fields ID.
ex. if we have field <input type="submit" name="page-1" id="page-1" value="page-1">
then we can get its value as:
echo $_GET['page-1'];
page-1 is the input field name.
You can make 5 different forms for each button and change the action to
action="index.php?id=1"
Then use $_GET['id']
Also change id='blog_post_id' " to id='$blog_post_id' "
If I am guessing right what is your difficulty then
$blog_post_id = $_GET['id'];
$sql = "SELECT * FROM blog_posts where id='".$blog_post_id."' ";//I modified this line
$result = mysqli_query($conn, $sql) or die('cannot load');
I have a control panel in php and mysql that displays on page load. I have a checkbox on that page(default off, not stored in db or tied to anything other than being an option to show old items), that when i click it, i want the query to refresh the data with the new filter applied. Basically on load it show only active items, when they check the checkbox i want the page to reload showing outdated items.
My code is:
<form action="" method="POST">
<input type="checkbox" name="filter" value="true">
</form>
<?PHP
$showold = (isset($_REQUEST['filter']));
if ($showold === 'checked' )
{
$showold = "WHERE `MyTable` = '0'";
}
else
{
$showold = "WHERE `MyTable` = '1'";
}
$query_NowPouring = "SELECT * FROM MyTable $showold";
$result = mysqli_query($mysqli, $query_NowPouring);
if($result){
while($row = mysqli_fetch_array($result)){
#set variables here and display the info in table here.
}
}
?>
Your code (isset($_REQUEST['filter'])); is returning boolean like 0 or 1 not string and you are matching it with string at if ($showold === 'checked' ) .
Problem is you can not compare boolean with string.
Im making a small php webpage which I plan to use to track on which subjects a helpdesk receives calls. My database has 3 important fields: id, name, and amount for each subject.
On my page I have a form with a dropdown list where you select a type of call and click submit. The idea is that every time you click submit the page reloads and the amount in the database for the chosen id is heightened by 1.
The form gives me the id and name for each call:
<form method="post" action="index.php">
<select class="select" id="calltype" name="calltype">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value=".$row["ID"].">".$row["NAAM"]."</option>".PHP_EOL;
}
}
?>
</select></br>
<input class="input" type="submit" name="Submit" value="Submit">
</form>
This part works, if I echo $_POST['calltype'] I get the correct ID. What I can't get to work is the update statement which I want to heighten the counter, like:
if(isset($_POST['calltype']{
mysqli_query("UPDATE calls SET amount=(amount+1), WHERE id = $_POST['calltype']");
}
How would I go about this? I tried several methods but can't get it to work
besides for the extra comma, interpolation with the POST array like this is risky. maybe try:
mysqli_query("UPDATE calls SET amount=(amount+1) WHERE id = " . mysqli_real_escape_string($link, $_POST['calltype']) . " ;");
Every time it takes two clicks on the submit button to get the code to go, also just randomly it was triggering different codes that are setup the same way but with different names for the input and inside the $_POST. Am I using the $_POST right by setting the name of the input to the same thing?
here is the code
<?php
//If submit form was clicked
if(isset($_POST['intro'])) {
//Server side validation for security purposes
if($userpoints >= 100 AND $intro == 0 AND $lifeonmarsalbum == 0) {
mysqli_query($con,"UPDATE users SET points = points - 100 WHERE users.user_name = '$username' LIMIT 1");
mysqli_query($con,"UPDATE users SET intro = 1 WHERE users.user_name = '$username' LIMIT 1");
}
}
?>
<form method="post" action="index.php">
<?php
if ($userpoints >= 100 AND $intro == 0 AND $lifeonmarsalbum == 0) {
echo '<input type="submit" name="intro" value="100pts">';
} elseif ($intro == 1 OR $lifeonmarsalbum == 1) {
echo '<input type="submit" name="submit" value="100pts" disabled title="You already earned this track!">';
} else {
echo '<input type="submit" name="submit" value="100pts" disabled title="You need at least 100 points for this download">';
}
?>
You only output a name="intro" submit button when that first line of if() clauses is met. Most likely the first time you load this page, that condition isn't met, so there's no intro button. After the first submitt, the condition IS met, and you get name="intro" in the form, and the submit then "starts" working.