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)
Related
I have database table which contains a number of questions each with corresponding answer options (multiple choice question). I am able to use PHP to extract a single question (with answer options) from the database, and would like to display one question at a time with the corresponding answer options to the user.
In my index.php file, I show the user the question using:
<?php echo $question["questionText"]; ?>
And I show the answer options using:
<input type="radio" name="optradio" value="<?php echo $question["option1"]; ?> <?php echo $question["option1"]; ?>
<input type="radio" name="optradio" value="<?php echo $question["option2"]; ?>"> <?php echo $question["option2"]; ?>
My issue is that the number of answer options (which are in form of radio buttons) varies per question with a maximum of 9 options.
What is the best way of ensuring that the user sees only however many options they need to see?
Instead of manually writing the below:
<input type="radio" name="optradio" value="<?php echo $question["option1"]; ?> <?php echo $question["option1"]; ?>
<input type="radio" name="optradio" value="<?php echo $question["option2"]; ?>"> <?php echo $question["option2"]; ?>
Just use a foreach loop here:
<?php foreach ($question as $option) { ?>
<input type="radio" name="optradio" value="<?php echo $option; ?>"> <?php echo $option; ?>
<?php } ?>
You should really do the following:
Retrieve the answers from the database.
(Optionally) Store it in an array.
Loop through it.
Echo out each option.
The above method will be both dynamic (each question has the right number) and also the right way.
Is it possible to echo php value into form label? Currently I can only echo into textbox which is editable for the user but I want it to be unable to edit like label, any way I can do so?
Below is for textbox which is able to work
<input style="color:#000000" type="text" value="<?php echo $account['Username']; ?>" name="name" required/>
I tried to echo in label but nothing shown in my web
<label for="fullname"><?php echo $account['Fullname']; ?></label>
Use the echo statement. Like this:
<label><?php echo ("this is a label");?></label>
Of course it is possible.
You can use the echo function like you used it before.
Make sure:
you are using PHP file (*.php)
index in array is not empty
you are using < ? php tags
Try this:
// index.php
<?php
$text = "privet";
?>
<label for="fullname"><?php echo $text; ?></label>
// output is <label for="fullname">privet</label>
read this http://php.net/manual/en/function.echo.php
be careful and read also this http://php.net/manual/en/function.htmlspecialchars.php
BTW: if it still does not working... just use dump to get the variable info
<label><?php echo $var; ?></label> // does not working
// ok -> then dump the var!
<?php var_dump($var); ?> // hmm.. lets see (maybe null or string(0)?) :-)
I know there have a been a lot of questions like this but none (that I have found, and I've looked at many, but correct me if I am wrong) have solved my problem.
As the title of the question suggests, I need to Post select options to another page. My code looks something like this:
<form id="join_pool_form" name="join_pool_form" action="connect.php" method="post">
<select name="pool_name" style="width:170px">
<?php
//The options are from an array. This works fine.
foreach($poolnames as $value):
echo '<option value="">'.$value.'</option>';
endforeach;
unset($value);
?>
</select>
<input name="passcode" type="password" autofocus required id="passcode" size="35"style="width:170px">
<input type="submit" id="submit" value="Join Pool">
</form>
The connect.php (action of the form) page has something like this:
<?php
if (isset($_POST['pool_name'])) {
echo "do this";
}else {
echo "do other";
}
So the result of this should be "do this", but I always get "do other".
I'm very new to this, so excuse me if the solution is extremely simple. However, I have looked over it multiple times to no avail.
Thanks in advance.
Thanks everyone for your help. Problem solved.
you just have to pass the value in the html attribute of the option:
<select name="pool_name" style="width:170px">
<?php
//The options are from an array. This works fine.
foreach($poolnames as $value):
echo '<option value="'.$value.'">'.$value.'</option>';
endforeach;
unset($value);
?>
</select>
<?php
if (isset($_POST['pool_name'])) {
$variable = $_POST['pool_name'];
}else {
$variable = 'Unknown';
}
replace
echo '<option value="">'.$value.'</option>';
with
echo '<option value="'.$value.'">'.$value.'</option>';
The value of the select option has to be set to what your intending to send. If you don't set it, it will send nothing.
<option value='test'>Dont choose this</option>
The above will send the value test.
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 }
}}
?>
I have this input form which goes like this:
<input type="checkbox" name="vehicle" value="Bike" />
I want to print it inside this variable:
$return .= '<div class="flink">'.$checkbox.'</div>';
or can i assign it to any other variable?
How can i do it? I tried putting :
$checkbox.= '<input type="checkbox" name="vehicle" value="Bike" />';
Just add:
$return = '<div class="flink">'.$checkbox.'</div>';
echo $return; // Somewhere on the page.
It looks like you're defining it fine, you just may not be echoing it out on the page.
If you're looking to print a variable to the webpage, you can use several different commands.
The echo command is useful when the variable is HTML.
The var_dump command is useful for debugging purposes, as it works on all kinds of variables.
print_r is also nice, because it formats the output in a way that's readable.
<?php
$q="select * from stores";
$res=mysql_query($q);
$StoreOPtion = '<option value="">--select--</option>';
while($rs=mysql_fetch_object($res))
{
$StoreOPtion .= '<option value="'.$rs->StoreId.'||StoreName:'.$rs->StoreName.'.. Street:'.$rs->StoreStreet.'..Phone:'.$rs->StorePhone.'..Email:'.$rs->StoreEmail.'..Area:'.$rs->StoreArea.'..Pin:'.$rs->StorePincode.'">'.$rs->StoreName.'</option>';
}
?>
<select name="StoreName" type="text" id="StoreName" onchange="getStoreAdd()"/>
<?php echo $StoreOPtion; ?>
</select>
Here i am using dropdown box. ypu sehe this example and use it