I'm creating a test system that is driven by Wordpress where each answer is input with a true/false text box to say whether it's the correct answer of not.
I've created a loop that outputs the answers with a checkbox next to it:
<?php if(get_sub_field('answer_options')): ?>
<?php while(has_sub_field('answer_options')): ?>
<p class="contact-form">
<input style="width: 20px;" type="checkbox" name="CheckboxGroup<?php echo $counter; ?>[]" value="<?php echo the_sub_field('answer'); ?>" />
<?php echo the_sub_field('answer'); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
How can I add code to that to include whether the answer is the correct one? I can do a conditional statement like the following to check which answer is correct but how can I incorporate that with the code above?
It needs to check which is the correct answer and also whether the user has ticked the correct/incorrect checkbox.
if( get_sub_field('correct') )
{
echo "do something";
}
else
{
echo "do something else";
}
You can't check a user's input directly with php like this.
If you're outputting a form generated by html, it will need to be completed, and then submitted back to the web server.
You can check the answers when they submit the form. It sounds like you want to keep the same form and just mark questions as correct/incorrect.
Solved with the following:
<?php
if( is_array( $_POST['CheckboxGroup'.$counter] ) ) {
foreach($_POST['CheckboxGroup'.$counter] as $value[$counter]) {
if ($answer == $value[$counter]) { ?>
<p><?php $score++;echo $value[$counter]; ?></p><br />
<?php }
}}
?>
Related
This question already has answers here:
Is there a pretty print for PHP?
(31 answers)
Closed 7 months ago.
I'm trying to print multiple values user has selected on form submit. However with following what I'm seeing is only the last element printed irrespective whether it is selected or not.
Note that the print on the screen I'm looking at is a print that a layman can understand!
<?php
if(isset($_POST['submit'])) {
//I'm trying to show the user these are the values you've selected
print_r($option['name']);
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>">
<td class="container">
<select multiple name="mercha_A[]" class="selectpicker form-control" title="Merchandiser type">
<?php foreach ($options as $option) { ?>
<option value="<?php echo $option['value']; ?>" <?php echo (isset($_POST[ 'mercha_A']) && in_array($option[ 'value'], $_POST[ 'mercha_A'])) ? ' selected="selected"' : ''; ?>>
<?php echo $option['name']; ?>
</option>
<?php } ?>
</select>
</td>
<td><button type="submit" name="submit">Submit</button></td>
</form>
Anyone needs a coffee on my account?
wrap your print_r in <pre> Tags
echo "<pre>";
print_r($option['name']);
echo "</pre>;
echo "<pre>";
print_r($_POST['mercha_A']); // you have to print the name attribute not option
echo "</pre>;
depending on your situation, you could use either of these, I think the last will best suite those who don't have a programming background. Because, I think JSON is a human readable format.
Method 1:
echo '<pre>'; print_r($_POST['mercha_A']); echo '</pre>';
Method 2:
echo json_encode($_POST['mercha_A']);
I think you meant to print
$_POST['mercha_A'];
Otherwise, $option['name'] is completely undefined in your case, but even if you put the print_r() at the end of the script, it would only be the name of the last option in $options.
In order to make print_r() readable, you can View Source (Ctrl+U) in your browser, or wrap it in <pre></pre> tags.
Using extbase debugger from TYPO3.
Check it out it's insane :) https://github.com/TYPO3/TYPO3.CMS/blob/master/typo3/sysext/extbase/Classes/Utility/DebuggerUtility.php
It helps you to debug arrays and object in a readable way
DebuggerUtility::var_dump($array)
I have the following code:
<div><input type="checkbox" name="fhaac_publicly_queryable" value="<?php
if (isset( $_POST ['fhaac_publicly_queryable'])) {
echo "checked";
} elseif ($fhaac_publicly_queryable == "on") {
echo "checked";
}
?>" name="fhaac_publicly_queryable" /> Publicly Queryable on a search</div>
I have the content saving to the database, but on save, the checked check box disappears and I can't figure out how to query so it returns. Any ideas?
Thanks in advance
You're writing the checked attribute as the value of the value attribute. Plus, the name attribute is written twice. Do this instead:
<div>
<input type="checkbox" name="fhaac_publicly_queryable" <?php
if (isset($_POST['fhaac_publicly_queryable'])){
echo 'checked';
} else if($fhaac_publicly_queryable == "on"){
echo "checked";
}?>>
Publicly Queryable on a search
</div>
I still encourage you to please do the PHP logic somewhere else, because this just looks awful. It really does. And to edit your if/else, it could be improved with or (||).
I have a couple of checkboxes and I want to execute a function for each of the elements marked as checked. How can I do this?
In my particular case, I think this might be a little bit easier, because the checkboxes are constructed like this:
$get_json_values=json_decode($json_string,true);
foreach ($get_json_values as $key=>$getlikes) {
if($getlikes['type']=='like') {
?>
<div>
<input type="checkbox" name="<?php echo $getlikes['name'] ?>" value="<?php echo $getlikes['id'] ?>" checked>
<a href="https://www.facebook.com/<?php echo $getlikes['id']; ?>" target="_top">
<?php echo $getlikes['name'] ?> </a>
</div>
<?php
}}
?>
So I think it should look like this:
function doIfChecked()
{
foreach ($get_json_values as $key=>$getlikes) {
if($getlikes['type']=='like'&&<sequence that checks if checkbox is checked>)
{//do stuff}
}
}
Can anyone please help?
Thanks to Julian H. Lam, I found a good answer, but my problem is that for each of the elements , I need to do a request to the server (like a page on facebook), but I can't put any php inside javascript.
So, how could I do that since php is not allowed inside js?
Using javascript:
var checkboxes = document.querySelectorAll('input[type="checkbox"]'),
numCheckboxes = checkboxes.length,
x;
for(x=0;x<numCheckboxes;x++) {
if (checkboxes[x].checked === true) {
// add code here to be done for each checked element
// you can refer to the checked element by calling "checkboxes[x]"
}
}
Well then, this is likely to be the n-th time someone is asking this, but honestly I didn't grab anything useful spending the last hour or so on Google. What I want to do is rather trivia, or so I thought. I have this working in Java Script but want to move it to PHP. In brief:
declare a var with a static value
add text field into which user is asked to enter value of above var
check if field is a) empty, b) non-empty mismatch, or c) non-empty match
My (limited) PHP wisdom has lead me into believing it ought to be something like the below, but apparently it's not. I'd very much appreciate any insight, tha.
<?php
$coconew = "blah";
if (isset ($_POST["cocosub"])) {
if ($_POST["cocoval"] == "") {
echo "empty";
} else {
if ($_POST["cocoval"] != $coconew) {
echo "mismatch";
} else {
echo "match";
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" id="cocosub" method="post">
<div>
<?php echo $coconew; ?>
<input type="text" id="cocoval">
<input type="submit">
</div>
</form>
You need to change
<input type="text" id="cocoval">
to
<input type="text" name="cocoval">
There are other (and probably better) ways to do this, but you are on the right track.
$_POST only looks for the name attribute of form elements, so modify your form as such:
<?php
$coconew = "blah";
if (isset ($_POST["cocoval"])) {
if ($_POST["cocoval"] === "") {
echo "empty";
} else {
if ($_POST["cocoval"] !== $coconew) {
echo "mismatch";
} else {
echo "match";
}
}
}
?>
<form id="cocosub" method="post">
<div>
<?php echo $coconew; ?>
<input type="text" id="cocoval" name="cocoval">
<input type="submit">
</div>
</form>
(I made a few other changes, you want to check isset on the element, not the form, it will POST to the same page if you don't give it an attribute [so no need to add the echo], and adding better type checking in your php)
in addition to the other answers already posted, you might also be interested in PHP's session support (depending on how "static" you need your static variables to be). That's where you'd put $cocoval and any other variables if you need to save their values across multiple requests for the same URL by the same user. See here for more info:
http://php.net/manual/en/features.sessions.php and
http://www.php.net/manual/en/book.session.php
This works:
<?php
session_start();
if(isset($_POST["cocosub"])){
$input = trim($_POST["cocoval"]);
if($input == ""){
echo "empty";
} elseif($input != $_SESSION["coconew"]){
echo "mismatch";
} else {
echo "match";
}
}
$_SESSION["coconew"] = substr(md5(uniqid()), 0, 5);
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="cocosub" method="post">
<div>
<?php echo $_SESSION["coconew"]; ?>
<input type="text" id="cocoval" name="cocoval">
<input type="submit" name="cocosub">
</div>
</form>
You needed to add name="cocosub" to the Submit button element in order for the first if(isset(...)) condition to be true. That's why the script didn't work. Also, instead of id, you need to use the name="cocoval" in the input text field as well in order for it to carry over into $_POST.
I'm doing a site with a voting system. What i want is to disable all input buttons (the ability to vote) if the user isnt logged in (ie. a session doesnt exist). How do i do a check in PHP at the top of the page and then allow/disallow the input buttons? Would i use CSS or jQuery?
Somewhere in the code check if the session is not set:
if(!isset($_SESSION['some_key'])){
$disable = true;
}else{
$disable = false;
}
Then, in the html:
<input type="radio" name="" value=""<?=($disable ? " disabled=\"disabled\"" : "");?>/> Vote 1
<input type="radio" name="" value=""<?=($disable ? " disabled=\"disabled\"" : "");?>/> Vote 2
<input type="radio" name="" value=""<?=($disable ? " disabled=\"disabled\"" : "");?>/> Vote 3
But you still have to check at the serverside before you accept the vote, if the person has voted before, because this form can be edited easily to post the data again and again.
<?php if(!isset($_SESSION['logged']))
{
echo "<script language=\"javascript\">";
echo "$(document).ready(function()
{
$('input[type=submit]').each(function() { this.attr('disabled', 'disabled') });
});</script>"
}
?>
You should dynamically generate a correct HTML code. Something like this:
<?php if(isset($_SESSION['logged'])): ?>
<form> voting form </form>
<?php else: ?>
<p>Sign in to vote</p>
<?php endif ?>
You should also check whether user is logged in before you process a form:
if (isset($_SESSION['logged']) && isset($_POST['vote'])) {
// process form
}