im trying to make something, and now i came to the part where i created a button that should lock the topic, but i don't know how to make it work like submit form, when i press it it should lock the topic, but nothing happens:
echo '<br><div class="ticket_info">';
if($stanje == 0 ) {
echo ' <button class="lockticket" name="lockticketbutton">Zatvori</button></a> ';
}
else if($stanje != 0 ) {
echo ' <button class="lockticket" name="unlockticketbutton">Otvori</button></a>';
}
echo '
Postavio: '.$output['Postavio'].' --- ['.$output['Naslov'].']
</div>
</br><div class="ticket_info2">
'.$output['Text'].'
</div>
';
And this is what it should output:
if(isset($_POST['lockticketbutton']))
{
$id = $_GET['id'];
$query = mysqli_query($con, "UPDATE `Dashboard` SET `Status` = '1' WHERE `ID` = '$id'");
if($query)
{
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else
{
header('location: dashboard.php');
}
}
you can submit the button by using following code
<form action ="your php file name" method ="post">
<button type= "submit" class="lockticket" name="odgovoritiket" >Otvori</button>
</form>
To use a button as Submit you need to put it into form.
Note: according to w3schools:
If you use the <button> element in an HTML form, different browsers may submit different values. Use <input> to create buttons in an HTML form.
Related
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 have form with submit button ,when i submit the form the value of the button should insert into the databse and change text of button to inserted.if the insertion is not possible(violates constraint) change the text of button to not inserted
<html>
<form action="" method="post">
<button type="submit" name="button1" value="one" id="bttn">CLICK</button>
</form>
</html>
<?php
if(isset($_POST['button1']))
{
$a=$_POST['button1'];
$con=mysqli_connect("localhost","PROJECT8","PROJECT8");
mysqli_select_db($con,"PROJECT8");
$rs=mysqli_query($con,"insert into ruff values('$a')");
if($rs)
{
//if data is inserted i need to change the button text to "inserted"
}
else
{
//change the button text to "not inserted"
}
}
?>
i searched about and it shows you need to use AJAX i am a newbie in AJAX
the complete code will be very helpfull
Try below code in your case,
if($rs) {
echo "<script>
document.getElementById('bttn').innerHTML = 'inserted';
</script>";
}
else {
echo "<script>
document.getElementById('bttn').innerHTML = 'not inserted';
</script>";
}
I have a PHP form set like this
<?php if (isset($submitted)) {
$output = checkData();
if (!is_null($output))
{
echo '<script type="text/javascript">alert("' . $output . '"); </script>';
}
else
{
createMeeting();
echo '<script type="text/javascript">alert("You meeting has been created. All of the recipients should shortly receive an email"); </script>';
header('Location: index.php');
}
} else { ?>
<center>
<form method="POST">
...
<input type="submit" name="submitted" value="Create Meeting">
</form>
<?php
}
?>
When I run it through a PHP code checker (codechecker website), no errors are returned. However, when I click on the submit button, the isset($submitted) code never seems to be executed (I've tested this by adding some echo statements in that section of the code).
If I click on the submit button, the form is cleared, so something is happening. I've put a number of different actions in, but the code is still not hit.
This if (isset($submitted))... should be if (isset($_POST['submitted']))...
You can check if the form is submitted using:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
Or check if an individual element of the form has been submitted checking if the name is in the post array using:
if(isset($_POST['submitted']))
I have a simple button that hides once the event is triggered
<?php
//Define attributes
echo'<input type="submit" id="toggler" name="add_friend"class=button
value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>
</input>';
?>
//Hide the button
<script>
var hidden = false;
function action() {
if(!hidden) {
document.getElementById('toggler').style.visibility = 'hidden';
}
}
The above works as it should no problems , However when I add form to get method=POST for the button does not hide nor does my POST make it to $_POST['add_friend']
echo ' <form method="post" >
<input type="submit" id=toggler name="add_friend" class="button"
value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>
</input>
</form>';
How can I make correct this so that the button hides and my POST is passed on to my isset code please .
if (isset ($_POST['add_friend'])){
//rest of my code once the button is clicked and hidden
Thanks in advance .
Your JS is most likely hiding the element. Then your form gets submitted (the POST), only for the page to refresh and the button reappear.
It seems to me that you want to hijack form submission and process the request with ajax.
The following example code shows a similar problem with the Php form processing. You could adapt to your liking (I have left out the required Javascript):
<?php
$feedback = null;
$people = array(
1 => 'Samuel',
2 => 'Fred',
3 => 'Roger',
4 => 'Mavis'
);
$friends = array(3); // i.e. Roger
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$add_friend = isset($_POST['add_friend']) ? $_POST['add_friend'] : null;
if(array_key_exists($add_friend, $people) && !in_array($add_friend, $friends)) {
array_push($friends, $add_friend); // You probably want to save state here
$feedback = 'Added ' . $people[$add_friend] . ' as friend.';
}
}
?>
<?php echo isset($feedback) ? $feedback : ''; ?>
<form method="post">
<?php foreach ($people as $key => $person) { ?>
<button name=
"add_friend" onClick=
"action();" value=
"<?php echo $key ?>"
<?php echo in_array($key, $friends) ? 'disabled' : '' ?>
>
Friend <?php echo $person ?>
</button>
<?php } ?>
</form>
Checkboxes may be a better fit than buttons here.
A couple things wrong with this. You have an extra double quote before 'method' in your form, and should also add action="#" to the <form> tag. This tells the browser to send the result of the form to the current page. It's also good practise to add a hidden field to send your data, rather than adding it to the submit button. Try this and see if it works.
if (isset($_POST['add_friend'])) {
var_dump($_POST['add_friend']);
}
echo '
<form method="post" action="#">
<input type="hidden" name="add_friend" value="'.$output1['username'].'">
<input type="submit" id="toggler" class="button" value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>
</form>
';
Bear in mind this will essentially reload the page, so if you want to make an asynchronous request (EG, send some request without loading the page again) you will need to look into a solution with AJAX.
unless you didn't supply more information or copy/paste everything, you seem to have extra quotations here:
echo ' <form "method="post" >
Wrapping the input in a form will natively submit the form as well as fire your javascript. If you want to use an ajax solution, tie into the submit event and prevent the default action. (using jquery here):
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
// serialize() will get the form data, which can be used in ajax call
console.log( $( this ).serialize() );
});
I have database generated content in a table with edit and delete buttons in a form at the end of the row.
<td>
<form action="?" method="post">
<div class="">
<input type="hidden" name="id" value="<?php htmlspecialchars($item['id']); ?>">
<input type="hidden" name="action" value="edit_daily_shift_report">
<input type="submit" value="Edit" onclick="return confirm('Edit this report?');">
<input type="hidden" name="action" value="delete_daily_shift_report">
<input type="submit" value="Delete" onclick="return confirm('Delete this report?');">
</div>
</form>
</td>
If I remove the delete button, the edit code works fine. But with both buttons present, the edit button fails and the item in the row is deleted. I am stumped. Not only is the action value of the edit button ignored, the action value of the delete button is executed. Any help is appreciated!
Here is the controller edit and delete code:
/*********************** Edit Daily Shift Report ************************/
if (isset($_POST['action']) && $_POST['action'] === 'edit_daily_shift_report')
{
include '../includes/dbconnect-local.php';
try
{
$sql = 'SELECT * FROM daily_shift_report WHERE id = :id';
$s = $db->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting data.' . $e->getMessage();
include 'error.html.php';
exit();
}
// Assign page title value
$pageTitle = 'Edit Daily Shift Report';
// Store single row resut in $item associative array
$item = $s->fetch(PDO::FETCH_ASSOC);
// Display report content in form
include 'edit_daily_shift_report.html.php';
exit();
}
/********************* Delete from Daily Shift Report *******************/
if (isset($_POST['action']) && $_POST['action'] === 'delete_daily_shift_report')
{
include '../includes/dbconnect-local.php';
try
{
$sql = 'DELETE FROM daily_shift_report WHERE id = :id';
$s = $db->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting data.' . $e->getMessage();
include 'error.html.php';
exit();
}
echo '<span style="color:#ff0000;">Daily Shift Report DELETED successfully!</span>';
}
Thank you.
You need to understand how the post request works. As you know, you have two action fields, one for delete and one for edit.
Your problem is in short that there is no way to Connecticut specific input fields to different buttons.
What i would rather suggest is that you set the name of the buttons to action and the value as the value you already use for the hidden fields.
With this you also have to make a minor change in your html. Instead of:
<input type="submit" value="something" onclick="something">
Use this:
<button name="action" value="edit" onclick="something">Edit</button>
And the same goes for delete button
When you use button tag instead of input, you can set a value to the button which is different from the display, which makes it a cleaner PHP code when checking the value of $_POST['action'] afterwards