I wanted to get value by clicking button. But it isn't working. Here is my code:
players.php
(header)
<?php
kick_ban(#$_POST['submit']);
getMsg();
?>
(Form)
<form method="POST" action="?go=players">
<input type="submit" name="kick" class="btn btn-warning" type="button" value="Wyrzuć" />
<input type="submit" name="ban" class="btn btn-danger" type="button" value="Zbanuj" />
<?php $idgracza = $sValue['playerid'] ?>
</form>
functions_admin.php
function kick_ban($post) {
require_once "../inc/SampRcon.class.php";
$config = getData('../inc/config.php');
$port = $config['port'];
$adrip = $config['adresip'];
$query2 = new SampRcon(''.$adrip.'', $port, "Modding1");
if ($query2->connect()) {
if(isset($_POST['kick'])){
$query2->kick($idgracza);
$_SESSION['success'] = 'Gracz o id '.$idgracza.' został pomyślnie wyrzucony z serwera.';
}
if(isset($_POST['ban'])){
$query2->ban($idgracza);
$_SESSION['success'] = 'Gracz o id '.$idgracza.' został pomyślnie zablokowany.';
}
}
else
{
$_SESSION['error'] = 'Błąd';
}
$query2->close(); // Close the connection
One problem is this line:
kick_ban(#$_POST['submit']);
You are only sending the submit variable (which is actually undefined) to the function.
What you want is this:
kick_ban($_POST);
Now I'm not saying this is a good way to code but that will send the whole post array into your function so you can access all the variables.
Another issue is when you try to access things in the kick_ban function:
$_POST['kick']
and
$_POST['ban']
should be:
$post['kick']
and
$post['ban']
The reason is that once you pass them into the kick_ban function the name of the array becomes $post instead of $_POST.
You need a hidden input to add $idgracza to the form:
<input type='hidden' name='idgracza' value = '".$sValue['playerid']."'>
Then access it in your kick_ban function: $post['idgracza']
Related
I have a website where I want to prevent that the user gets to a specific page trough the url. So I only want it to make accessible when they pressed on the submit button on the first page.
The problem is that with this code I automatically go to the index.php?page=home, while $_POST is not empty. If $_POST is not empty I want the user to get on the other page.
This is my html:
<form action="index.php?page=cart" method="post">
<input type="hidden" name = "submit" value = "submit" />
<button type="submit"
name="action"
value="details"
class="btn btn--big btn--big-2 btn--dark">
your details ->
</button>
</form>
This the code I use in the controller:
public function detail() {
if(empty($_POST['submit'])) {
header('Location: index.php?page=home');
exit();
}else {
//Rest of code..
}
When I look in the debugger, there is a $_POST with the value submit, thus this is why I find it so strange.
I'm trying to display one of two buttons depending on "magnum"(printername) being in present in the array "booleans".
My problem is that when the form gets posted, the data retrieved on page load is correct, but the buttons displayed are incorrect. if clicked on a button, the form posts and refreshes the page, "magnum" gets pushed into $_SESSION['booleans'] but the button still displays "btn btn-default", so it requires another page refresh for the button to load correctly('btn btn-succes').
Is my problem due to $_SESSION or am i missing something?
echo'
<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
if(isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']){
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans'][$pos]);
dump('unset');
}
}
elseif(!in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
if(isset($_POST['submitMagnum'])&& $_POST['submitMagnum']){
array_push($_SESSION['booleans'],'magnumBool');
dump('set');
}
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '
</div>
</form>';
$_SESSION['printers'] is an array containing "magnum" -
$_SESSION['booleans'] is the array which isn't working as i would like it to -
$enabled_printer = "btn btn-success" <br>
$disabled_printer = "btn btn-default" <br>
$lost_connection_printer = "btn btn-danger disabled"
The problem is that you are mixing elaboration and printing. Try to split you code, so it will work and it will be more readable:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']) {
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans']['magnumBool']);
} elseif (isset($_POST['submitMagnum'])&& $_POST['submitMagnum']) {
$_SESSION['booleans']['magnumBool'] = true;
}
}
echo'<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(isset($_SESSION['booleans']['magnumBool'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
} else {
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '</div>
</form>';
P.s. note the use of "magnumBool" as a key isset instead of as a value: in this way (when possible) you will avoid duplicate entry and will make you code lighter if you have large arrays ;)
P.p.s. try always to keep you login separate from you template, this will make your code more readable and easier to maintain
I've been having some problems with a bit of PHP running when a form is submitted.
I'm trying to have a variable be determined by which submit button is being pressed and grabbing the $_POST['name'] to carry out some SQL select. There are currently two submit buttons in my form, reading YES and NO.
The problem I'm having is that I keep getting the same answer from my query, but when I test in my database it works just fine. It seems to be something in my if/else statement.
<?php
require_once ("/includes/session.php");
require_once ("/includes/db_connection.php");
require_once ("/includes/functions.php");
require_once ("/includes/validation_functions.php");
if ($_POST['answer1']) {
$chosenAnswer = 1;
} else if ($_POST['answer2']) {
$chosenAnswer = 2;
}
$query = "SELECT questionFlow.NxtQ FROM ";
$query .= "questionFlow WHERE area = '{$_SESSION['probArea']}' ";
$query .= "AND ANSid = '{$chosenAnswer}' AND Qid = '{$_SESSION['x']}'";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
print_r($row["NxtQ"]);
}
?>
<form action="Assets/ajax/questions.php" id="ajax-questionset" method="post">
<br />
<br />
<?php echo($howmany); ?>
<br />
<br />
<div><?php echo($currentquestion); ?></div>
<span>
<button class="btn btn-hero" type="submit" name="answer1" value="1"/><?php echo($answer1); ?></button>
<button class="btn btn-hero" type="submit" name="answer2" value="2"><?php echo($answer2); ?></button>
</span>
</form>
As per Bug's suggestion below, I've added in the javascript and tested with an alert which is now giving me the right value, but it's messed up my ajax and it's now loading the php page rather than doing this 'blind' (for want of a better word)
Any suggestions?
function buttonB_clickHandler(event) {
document.getElementById('hiddenId').value = 2;
document.getElementById('ajax-questionset').submit();
}
Resolved this by taking out the javascript .submit and applying it to the buttons them self to read type="submit".
Thanks again for all your invaluable feedback guys, I love this site, great community!
Read my comment and try something like this:
<input id="buttonA" type="button" value="do something" onclick="buttonA_clickHandler(event);"/>
function buttonA_clickHandler(event) {
document.getElementById('hiddenId').value = whatever;
document.getElementById('theForm').submit();
}
repeat the code for the other button changing the name of function and other code.
Try adding {} to the else statement and adding isset
if(isset($_POST['answer1'])){
$chosenAnswer = 1;
}
else {
if (isset($_POST['answer2'])){
$chosenAnswer = 2;
}
}
Currently I am on a question to store session and pass it to the next form, but as hard as I try to start session and store the variable inside the session, it does not work. Please shed some light on this, thanks!
MainForm.php
<?php
session_start();
require("inputValidation.php"); // This php file is just a file that does validation for my side
$validForm = true;
if ($_POST)
{
}
?>
<form class="form" action="nextform.php" role="form" method="post">
<input type="text" class="form-control" id="name" name="name" value= "<?php
if (isset($_POST['name']))
{
if (!validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = false;
}
if (validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = true;
}
}
?>
<input name="submit" type="submit" value="Submit" class="btn btn-primary">
nextform.php
print_r ($_SESSION);
The problem is even if i enter any value inside the textbox name, I will be just redirected straight to nextform.php without getting my session value. Why is this so? Is there any way I can get my session value without changing action="nextform.php"?
Thank you!
Sorry I am still new to sessions and PHP so bear with me :)
Basically what you wan't to do is move the whole
if (isset($_POST['name']))
{
//... you code
}
to the page where the form is submitted, in your case nextform.php.
Your pages should be something like:
MainForm.php
<form class="form" action="nextform.php" role="form" method="post">
<input type="text" class="form-control" id="name" name="name" value= "">
<input name="submit" type="submit" value="Submit" class="btn btn-primary">
nextform.php
<?php
session_start();
require("inputValidation.php"); // This php file is just a file that does validation for my side
$validForm = true;
if (isset($_POST['name']))
{
if (!validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = false;
}
if (validateRequired($_POST['name']))
{
$_SESSION['test'] = $_POST['name'];
$validForm = true;
}
}
if($validForm) {
echo "Validation success";
print_r ($_SESSION);
} else {
echo "Form validation failed.";
}
?>
Basically you can access the POST parameters in the page where the form is submitted, see action="nextform.php".
Here you can check if the form was submitted and do the proper validation, along with redirecting the user somewhere, or just showing the appropriate messages as needed, ie Validation failed or Validation success.
How do i implement button click php when either one of the buttons is clicked? I am trying to do is, if sumbit button is clicked the php script below shows it written to a file and if view comments is hit shows comments
<html>
<body>
<form action="http://localhost/class/assignment10.php" method="post">
User Name: <input type="text" name="uname"><br>
User Comments: <textarea type ="text" name="tcomment"></textarea><br>
<input type="submit" value="Submit Comments"/>
<input type="submit" value="View Comments"/>
</form>
</body>
</html>
I have two form buttons IF submit comments is clickedthe php code should write the user’s name and comments to a text file (use fputs function to write to a file. Also you should insert a newline after each name and each comment). When user’s name and comments are successfully recorded, php should direct
On the other hand, if a user clicks on the ‘view all comments’ button, the php code should
show all users and their corresponding comments (use fgets function)
<html>
<head>
<title>PHPLesson10</title>
</head>
<body>
<?php
// Collect user name and add a line break at the end.
$customer = $_POST['uname']."\n";
// Collect comment and add a line break.
$comments = $_POST['tcomment']."\n";
$file = fopen("C:/data/feedback.txt", "a");
If ($file) // if the file open successfully
{
// Write the variables to the file.
fputs($file, $customer);
fputs($file, $comments);
fclose($file);
}
Else
{
Echo "Please try again.";
}
?>
</body>
</html>
<?php
// Open the file feedback for reading
$file = fopen("C:/data/feedback.txt", "r");
// While the end of the file has NOT reached
While (!feof($file))
{
// Print one line of file
Echo fgets($file);
Echo "<br />";
}
// close the connection
fclose($file);
?>
Your submit buttons should like below :
<input type="submit" name="submit_comment" value="Submit Comments"/>
<input type="submit" name="view_comment" value="View Comments"/>
And your php code for each buttons should like below:
<?php
if(isset($_POST['submit_comment']))
{
//the php code will be here for save comment
}
if(isset($_POST['view_comment']))
{
//the php code will be here for view comment
}
?>
use name attribute on buttons
<input type="submit" value="Submit Comments" name="btn_submit"/>
<input type="submit" value="View Comments" name="btn_view"/>
then on php you can check something like
if (isset($_POST['btn_submit']))...
or
if (isset($_POST['btn_view']))...
Best regards,
Nebojsa
For buttons, I usually use <button> tags and not <input> tags, as long as you're using <!doctype html> for HTML5.
<form action="http://localhost/class/assignment10.php" method="post" id="commentForm">
<button type="submit" name="action" value="submit" form="commentForm">
Submit Comments
</button>
<button type="submit" name="action" value="view" form="commentForm">
View Comments
</button>
</form>
Then use PHP to figure out what the user clicked like this:
<?php
$action = $_POST["action"];
if ($action == "submit") {
// submission code
}
if ($action == "view") {
// viewing code
}
else {
die("Your request could not be completed.");
}
?>
First give your submit buttons a name like so:
<input type="submit" name="button1" value="Submit Comments"/>
<input type="submit" name="button2" value="View Comments"/>
and then use this PHP:
<?php
if (isset(#$_POST['button1'])) {
//Code to execute
}
if (isset(#$_POST['button2'])) {
//Code to execute
}
?>