I am currently working in PHP I am new to development in PHP and I am having quite a time trying to get a form to submit then update a value each time it is submitted.
So here is the gist of it. I am submitting my form to self at which time it runs this:
<div id="quiz">
<form method="post" id="test" action="">
<input type="hidden" value="<?php $question->set_results() ?>">
Answer A<input type="radio" name="answer" value="a">
Answer B<input type="radio" name="answer" value="b">
Answer C<input type="radio" name="answer" value="c">
Answer D<input type="radio" name="answer" value="d">
<input type="submit">
</form>
class test_results{
public function get_results(){
$this->lastanswer = array();
$this->personsanswer = $_POST['answer'];
$this->allanswers = array_push($this->lastanswer, $this->personsanswer);
implode($this->lastanswer);
echo $this->allanswers;
}
}
After this runs it seems to work but instead of updating the variable it just adds a number where I wanted it to update. All in all I just want to be able to submit a form, after submitting a form I have a hidden field in the form and I want it to update that hidden field with the ALL of the previous options chosen. I have gotten it as far as to update one option at a time but not multiple times.
My end goal is to have a questionnaire form where users fill out answers one question at a time and each time they submit the form the hidden field holds ALL of the previous answer letters in it.
set_results() is not specified in the question.
You will need to take into account $_POST["answer"] inside set_results. Also, you will need to post the hidden field as well, otherwise you will lose your information. So, first, change your form to post the hidden field as well:
<div id="quiz">
<form method="post" id="test" action="">
<input name="answers" type="hidden" value="<?php $question->set_results() ?>">
Answer A<input type="radio" name="answer" value="a">
Answer B<input type="radio" name="answer" value="b">
Answer C<input type="radio" name="answer" value="c">
Answer D<input type="radio" name="answer" value="d">
<input type="submit">
</form>
and then you will need to calculate something like
$_POST["answers"].($_POST["answers"] ? "," : "").$_POST["answer"]
somewhere, so you will get the correct result, returned to the hidden field as value.
Related
Clarification:
I have a form let say like this:
<form method="post" action="https://www.example.at/example/" id="comparison">
<input type="radio" id="region" name="region" value="austria">
<input type="radio" id="region" name="region" value="germany">
<input type="radio" id="tarif" name="tarif" value="Basis">
<input type="radio" id="tarif" name="tarif" value="Comfort">
<input type="submit" name="submit" value="compare">
</form>
User can submit - working finely.
On a second page (https://www.example.at/example/) I want to pick up the variable region. But I would like the user to again be able to switch between Basis and Comfort. So value region does not to be entered again.
<form method="post" id="comparison2">
<input type="hidden" name="region" id="region" value="<?php echo_POST['region']; ?>" />
<input type="radio" id="tarif" name="tarif" value="Basis">
<input type="radio" id="tarif" name="tarif" value="Comfort">
<input type="submit" name="submit" value="compare">
</form>
So if I echo the php with region it gives me the correct value outside of the form. Altough after submitting the second form it triggers a php shortcode(again) with the submit again where it displays a € value from our database.
But even though it worked properly with the input entered by the user(first step) it doesn't use or differently use the hidden input.
If it helps: I'm currently testing here: https://www.krankenversichern.at/testing-environment/
So the solution was: I took all of my html code and put it in a shortcode. I used the php echo as suggested and used the text element widget in elementor. That's it. Took probably 2 minutes!
this is one radio button set
<input type="radio" name="image" id="100" value="a"/>
<input type="radio" name="image" id="100" value="b"/>
<input type="radio" name="image" id="100" value="c"/>
<input type="radio" name="image" id="200" value="d"/>
<input type="radio" name="image" id="200" value="e"/>
<input type="radio" name="image" id="300" value="f"/>
<input type="radio" name="image" id="300" value="g"/>
<input type="radio" name="image" id="400" value="h"/>
and so on...
and this is another radio button set
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>200
<input type="radio" name="number" value="300"/>300
<input type="radio" name="number" value="400"/>400
<input type="radio" name="number" value="500"/>500
<input type="radio" name="number" value="600"/>600
my question is how can i compare value of one with value="100" to other with id="100" in php
there is a comparison i want to make but for the field with id = 100 i have values as abcd.. so i cant assign it value = 100 cuz i m saving it in database
means..
there are other fields with id=100 but their values shall be diffrent otherwise i m not able to identify them after they are send to databse
still i have to do comparison of both.. and that too serverside
so what are my options?
more detailed explanation....
there are two sets of radio buttons..
in one set there are values..
and in other sets there are images related to those values..
when user clicks set of value... and user click on set of images
so php shall match that image to the value.. if user clicked on 100 as value and later he clicks on an image under the id 200
database shall not allow it to enter and gave him some error
Sorry Had to see what your talking about. Help to edit this so to understand your requirement, 'shall match that image to the value'
<input type="radio" name="image" class="100" value="a_100"/>
<input type="radio" name="image" class="100" value="b_100"/>
<input type="radio" name="image" class="100" value="c_100"/>
<br>
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>
<input type="radio" name="number" value="300"/>
<?php
$image = strstr($_POST['image'],'_');
$number = '_'.$_POST['number'];
if($image == $number) {
//Do something
}
The question is looking for to compare the two fields in PHP. Presumably this means that you want to examine them when the form is posted.
This means that you'll only have available to you the parts of the field that are sent via the POST -- ie the field name and the value. PHP will never see the id of any of the fields, nor the class, nor any other attributes in the HTML code other than name and value.
Therefore the discussion in the comments about whether or not to use id is a somewhat moot point -- even if it is a good point for you to know in general for your HTML code, you can't really use either id or class here anyway because PHP will never see them.
You need to take an entirely different approach.
[EDITED after question edit]
Add the 100 or 200, etc to the value of the image radio buttons, like so:
<input type="radio" name="image" value="a_100"/>
<input type="radio" name="image" value="b_100"/>
<input type="radio" name="image" value="c_100"/>
<input type="radio" name="image" value="d_200"/>
<input type="radio" name="image" value="e_200"/>
<input type="radio" name="image" value="f_300"/>
<input type="radio" name="image" value="g_300"/>
<input type="radio" name="image" value="h_400"/>
(The number radio button set remains unchanged from your question)
You can then read it in PHP as follows:
$number = $_POST['number'];
list($imgNumber, $imgValue) = explode('_',$_POST['image']);
You can now compare $number with $imgNumber.
Hope that helps.
It's worth asking a follow-up question though: What are you trying to achieve here? Is this a validation exersise? ie where you're checking that the user is selecting an image option that matches the number option he's picked? If that's what you're doing, you may find it's more user friendly to use Javascript to filter the options available when they select the number option so that the image radio button set only shows options that are valid for the selected number.
This would be an entirely different question, so I won't go into any detail here, but I would suggest that it might be a more user-friendly way of doing things if you did it like that.
I am developing a PHP website which will generate several approval requests.
My approval page has only one form and one save button, and I want to use 2 radio buttons for each request.
Here is a link to how I imagine it to look like: link
On the PHP side I have no problem with generating the form and assigning different names to the radio buttons. My problem is what names I should give to the radio buttons in order for me to be able to associate them to a specific member on the PHP side? Or do I need to set values for the radio buttons?
How would I accomplish this?
If you have an even better solution to handle such a case I am open to that as well.
Thanks
Every User will have a unique user_id,
As you are displaying radiobuttongroup for each other user in the page, and want to access then in php,
The base way to name them in a array with indices as their user_id
<input type="radio" value="1" name="acceptance['uid1']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid1]"/>Deny
<input type="radio" value="1" name="acceptance['uid2']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid2]"/>Deny
<input type="radio" value="1" name="acceptance['uid3']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid3]"/>Deny
in php
foreach($_POST['acceptence'] as $uid=>$acpt)
{
..Do whatever
}
I hope this would help..If something else is needed plz comment..
Here you are:
<input type="radio" value="approve" name="acceptance"/>Approve<br />
<input type="radio" value="deny" name="acceptance"/>Deny
With radio buttons, they should named the same and have different values for grouping them.
UPDATED
To me, I will add row id to each radio group buttons. For example
<input type="radio" value="19" name="acceptance1"/>Approve<br />
<input type="radio" value="0" name="acceptance1"/>Deny
...
<input type="radio" value="21" name="acceptance2"/>Approve<br />
<input type="radio" value="0" name="acceptance2"/>Deny
...
<input type="radio" value="23" name="acceptance5"/>Approve<br />
<input type="radio" value="0" name="acceptance5"/>Deny
Assuming we have 5 rows per page and each approve radio button holds id of a records in database.
<?php
$approve = array();
for($i=1;$i<=5;$i++)
$approve[] = $_POST['acceptance'.$i];
?>
Then you will get what row has approve selection (i.e value !=0). Hope this helps
I try to implement the follwing code:
<?php
$yesno1="";
$yesno2="";
$option1="";
$option2="";
$option3="";
if(isset($_POST['submit'])){
$yesno1=$_POST['yesno1'];
$yesno2=$_POST['yesno2'];
$option1=$_POST['option1'];
$option2=$_POST['option2'];
$option3=$_POST['option3'];
}
?>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<input type="radio" name="yesno1" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno1" value="no" style="outline:0;"/>No
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="outline:0;"/>No
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
<input type="submit" value="Submit" name='submit'>
</form>
Once I click submit button, normally the value I seleted in radio and marked in the checkbox are gone, so how can I keep the value even after the click the submit button or refrsh the page using php, javascript is fine as well, would be better if any one can help me with it on both php and javascript since I am not very good on both of them, thanks for kind help:)
An example below
<input type="checkbox" name="option1" value="Milk" <?php echo ($_POST['option1'] == 'Milk' ? 'checked' : '') ?>> Milk<br>
From MDN:
The input (<input>) element is used to create interactive controls for
web-based forms.
Attributes
type
[...]
radio: A radio button. You must use the value attribute to define the
value submitted by this item. Use the checked attribute to indicate
whether this item is selected by default. Radio buttons that have the
same value for the name attribute are in the same "radio button
group"; only one radio button in a group can be selected at one time.
This is a basic HTML question. Learn to walk before you run ;-)
A very basic question...
How can I only allow the selection of one option in a list of radio buttons?
<form action="process_repair.php" method="POST">
<label for "repair_complete">Repair complete</label>
<input type="radio" name="Yes" value="true">
<input type="radio" name="No" value="false">
</form>
When this code runs, it's possible to select both radio buttons, but I'd like them to interact so you can only select one or the other.
Any help much appreciated! :)
Give them the same name.
<form action="process_repair.php" method="POST">
Repair complete
<input type="radio" name="complete" value="true" id="complete_yes" />
<label for="complete_yes">Yes</label>
<input type="radio" name="complete" value="false" id="complete_no" />
<label for="complete_no">No</label>
</form>
Labels must have a for attribute, directing to the corresponding input's id.
Every input should have same "name"