I want to show buttons depending on the values from a table. Afterwards I need to access anyone button and act according to the label of buttons. To be more specific, i have a table containing names of subjects, I want to show the buttons each having label of a subject. when user clicks a button (eg: Physics), the screen should display a test (MCQs) of Physics only, and so on:
Below is my code:
<button id="subj" onclick="showSubjects()">Start Test </button>
<div id="subjects" style="display: none;">
<form method='POST'>
<?php include_once('connect.php');
$query = "select distinct subject from subjects order by subject
desc ";
$result = mysqli_query($conn,$query);
while($row=mysqli_fetch_array($result)){
$subjectname = $row['subject'];?>
<input type="submit" id = "sub" name="sub"
value='<?php echo $subjectname; ?>'>
<?php } ?>
</form>
</div>
<?php
if(isset($_POST['sub'])){echo $subjectname; } else {echo
"Nothing";}
?>
</body>
<script type="text/javascript">
function showSubjects(){
document.getElementById("subjects").style.display = "block";}
function showTest(){
document.getElementById("tests").style.display = "block";
} </script>
This shows 3 buttons: PHISICS CHEMISTRY BIOLOGY
when user clicks any of these buttons it takes it as last button value i.e. BIOLOGY.
How can I amend it so that if user clicks CHEMISTRY it should show "Chemistry button clicked" and so on
The only thing that will change based on the submitted form is the $_POST array. Other variables, like $subject, will be the same whatever the user does; PHP isn't going to guess what you want them to be.
You've already got logic checking if the user pressed any button:
if(isset($_POST['sub'])){ ... }
Determining which button they pressed is just a matter of looking at that same variable:
echo $_POST['sub'];
The same goes for any other form controls you add - the user's selections will end up in $_POST, and it's up to you to read them from there.
It's also worth remembering that the logic for displaying the form doesn't need to be in the same place as the logic for precessing the submitted form, so the fact that these buttons are generated dynamically doesn't make a difference to how you read the submitted data back. You can't even guarantee that the user used your form at all, they could write their own and submit it to your server, or edit it using their browser's debugging tools.
Related
I am trying to create an html form where i have a dropdown menu populated with data from stations table. I am able to select a station and then i want to use this station name when a button is clicked to display a table with related data. But i don't understand how i can use selected station to create another query when submit button is clicked. Below is my code:
<form method="post">
<label>Choose a station:</label>
<select name="owner">
<?php
$sql = mysqli_query($conn, "SELECT name FROM stations");
while ($row = $sql->fetch_assoc()){
?>
<option value="owner1"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
i think onclick must be used but i don't understand what would be posted that i can use for new sql query.
When you click submit button browser send form via POST method to the same page and reload it. So you can capture the value selected in the form with this PHP code:
If (isset($_POST['owner'])) {
// mysqli query where the selected value is $_POST['owner']
}
I am working on a restaurant search and review project. I have a search page that will show restaurant names and provides a link for the user to add a review for each restaurant result by using the hidden input rid.
In my review.php, I am using the value sent in for the hidden input rid to run another simple query and then display the name of the restaurant. Then I have several inputs so the user can input their name, their rating of the restaurant, and finally a comment.
<form action="comment.php" method="GET">
<INPUT TYPE="hidden" NAME="rid" VALUE="">
<?php
$db=mysqli_connect(//parameters);
$restaurant_id= $_GET['rid'];
if (!empty($_GET)){
$result = $db->query(//query);
$row = $restaurant_result->fetch_assoc()
echo $row["name"];
}
?>
<INPUT //other inputs for the user>
</form>
My problem is that when the user hits a submit button at the bottom, the page will refresh with the url www..../review.php?rid= meaning that the name of the restaurant can't be displayed since no restaurant id parameter is being resent in the form. How can I send the restaurant id back to the page again once it is done?
I tried the top answer in this thread but I get the same url problem: Pass parameter to form action in HTML
<INPUT TYPE="hidden" NAME="rid" VALUE="<?php if(isset($_GET['rid'])){echo $_GET['rid'];} ?>">
I have 2 forms right now. Form A, where I display the value inserted by user. (a user can insert as many value as they want). That's the reason why I put the radio button because at the end they need to choose only one to proceed. I have a button to save what a user has inserted.
Form B, where I have a button to go to another page that brings the value form the chosen radio button.
at formA.html
<form method="POST" action="formB.php">
<--your radio buttons goes here-->
<input type="submit" name="formAsubmit">
</form>
at formB.php
<?php
if(isset($_POST['formAsubmit'])){
//print user entered values on form A
?>
<form action="formB.php" method="POST">
<--your form B input fields goes here-->
<input type="submit" name="formBsubmit"/>
</form>
<php?
} elseif (isset($_POST['formBsubmit'])){
//Do what you want with formB informations
}
?>
Thats your dual forms
i got a bit of a problem, as you guys can see in my code, i got a foreach looping through my img folder, showing all the images, well i left out the proper styling of the images, since the problem is the form inside every image.
i need to get the value from the radio input, so i can tell my other script to select all the info from the DB where the ID has the same value as the input.
and i needed a if statement so that if nothing is set i can show something saying you need to select a radio before you can listen to any stations.
but my if statement keeps showing 'error', even tho i click on the buttons
i hope anyone could help me out :)
if(isset($_POST['submitRadio')):
$test = $_POST['radio'];
echo $test;
else:
echo 'error';
endif;
foreach(glob('img/radios/big/*.png') as path):
printf('
<form method="post">
<input class="hidden" type="text" name="radio" value="1">
<input type="submit" name="submitRadio" value="Go!">
</form>
');
endforeach;
after doing what GolezTrol told me to do:
<div class="row text-center">
<?php
$sql = "SELECT * FROM radio";
$stmt = $Db->dbh->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
if(isset($_POST['submitRadio'])):
$test = $_POST['submitRadio'];
echo $test;
else:
echo 'error';
endif;
?>
<form method="post">
<?
foreach($result as $radio):
printf('
<button type="submit"
name="submitRadio"
value="'.$radio['id'].'">
Go!
</button>
');
?>
</form>
I've removed my previous solution. I only just saw that you have no actual radio button but hidden elements, so basically the buttons are the radios.
That changes it a little bit because you actually have the alternative suggestion I proposed earlier: The buttons themselves work as radiobuttons that immediately post the chosen option.
To solve your issue, you can use a button instead of an input for submitting the form. In a button, you can make the caption and the value differ from each other, so you can just put the id (or filename, or whatever you need) in the value of the button and don't need the hidden input at all. The content of the button tag is the caption it gets, so the code can look like this:
if(isset($_POST['selectedFile')):
$test = $_POST['selectedFile'];
echo $test;
else:
echo 'error';
endif;
?><form method="post"><? // Open the form outside of the loop
foreach(glob('img/radios/big/*.png') as path):
// Not sure what you want to do here. Put the filename instead of
// "1" in the value?
$filename = basename($path, '.png');
?>
<button type="submit"
name="selectedFile"
value="<?=$filename?>">
Go!
</button>
<?
endforeach;
?></form><? // Close the form
In this example I've pulled the form tags out of the loop, because strictly you only need one form. But since there are now no radiobuttons at all, you could also have one form per button. It would still work.
By the way, I saw you mentioned a database in your comment. Of course I didn't take that into account, since you didn't mention that in the question. Anyway, it shouldn't change the basic concept, only the way the 'value' is determined.
First, the value code in radio tag is always 1. So if any radio button is selected, you will always gets 1. Instead the value clause should have the filename. So you get to know which image (radio button) is selected.
For none of the image (radio button) selected, you may use
if (!isset($_POST['radio']))<br>{<br>//php code }
I have a form in which i have two input button one for multiple delete and another for multiple suspend items. like this-
<form>
<input type="checkbox" name="check_item[]" value="<?php echo $users['user_id']; ?>" />
<input type="button" id="suspendall" name="suspendall" value="Suspend" />
<input type="button" id="deleteall" name="deleteall" value="Delete" /></td>
</form>
when I click on delete or suspend it ask for confirm that event by jquery like-
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?')){
$('#listing').submit();
}
});
if it confirm cancel form is not submitted and if it confirm OK form is submitted and on submission i have to delete or suspend that item from db. I have write this code for this-
if(isset($_POST['deleteall'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->deleteUser($usersId);
}
if(!empty($_POST['suspend'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->suspendUser($usersId);
}
the problem I am facing is both times when the form is submited i got only array of ids of check boxes. I am not able to identify which button is clicked because I am not getting button value. that why its not working, and if I changed these button into submit button its working very nice but didn't ask for confirm the event. What should I do for that. Do anyone have any solution for that, Please help me. thanks
When submitting with jQuery().submit() the button on which was clicked is lost.
You could try to not submit() the form in the click handler but instead call evt.preventDefault() when ! confirmed().
In you HTML form has a hidden form field.
On the JS event set the value of the hidden field before submitting
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?'))
{
$("#hiddenFormId").val("deleteAll");
$('#listing').submit();
}
});
Then use this type of code in your PHP (Sorry not a PHP programmer)
if $_POST['hiddenFormId'] == 'deleteAll'