trying to get a form output on a request - php

I am trying to get values from a form but on Submit request. Bellow is just the PHP code as I know that the HTML Form is correct as it worked perfectly when I did not place the isset() function into the PHP.
<?php
if (isset($_POST["submit"]))
{
$fname = $_POST['firstname'];
$emailstr = $_POST['email'];
$postaddrstr =$_POST['postaddr'];
$favsportstr =$_POST['favsport'];
$emailliststr =$_POST['emaillist'];
}
?>
I believe the error lies somewhere in the below part as i am getting an undefined variable message for $val and an Array to string conversion at the foreach loop.
<section id="output">
<?php
if (isset($_POST["submit"]))
{
echo "<h2>The following information was received from the form:</h2>";
echo "<p><strong>First Name:</strong> $fname </p>";
echo "<p><strong>Email = </strong> $emailstr </p>";
echo "<p><strong>Post Address = </strong> $postaddrstr </p>";
echo "<p><strong>Your Favourit Sport:</strong>
foreach($favsportstr as $val) {
$val
}";
echo "<p><strong>Email list = </strong> $emailliststr </p>";
}
?>
</section>

You can't write loops inside echo change following line:
echo "<p><strong>Your Favourit Sport:</strong>
foreach($favsportstr as $val) {
$val
}";
to
echo "<p><strong>Your Favourit Sport:</strong> ";
foreach($favsportstr as $val) {
echo $val;
}

This is where your error comes from:
echo "<p><strong>Your Favourit Sport:</strong>
foreach($favsportstr as $val) {
$val
}";
Just change it to this and it will be fine:
echo "<p><strong>Your Favourit Sport:</strong>";
foreach($favsportstr as $val) {
echo $val;
}
The reason is you can't put a loop inside an echo statement. Put echo inside the loop not the loop inside echo. 'echo' statement is just for printing the output. It wont support any processing it will just print everything inside it. It can only output the variable values when they are placed between double quotes. Like this : echo "$var";

Related

Echo php in php?

I would like to insert a PHP tag within a PHP tag in the example below:
<?php $values = get_field('sold');
if($values)
{ foreach($values as $value) {echo '<div id="sold">'; echo $value; echo '</div>'; } }
else {
echo '<div id="inquire">Inquire about this item...</div>';
}
?>
I would like to imprint the_title(); as the subject of the email.
Can anyone offer any insight?
If the title is the return value of a php function you can just call the function and concatenate it to the rest of the echo.
For example
echo '<div id="inquire">Inquire about this item...</div>'

Replace Values of an array with key's of another array. PHP Homework

Hi there this is a bit hard to word so I'm trying my best to explain what is happening. Pretty much I have a form where I had a selection box. I had to fill it with the following.
<?
$PROVINCES = array("--" => "---Please Select Provinces---",
"nf"=>"Newfoundland",
"pe"=>"PrinceEdwardIsland",
"nb"=>"New Brunswick",
"ns"=>"Nova Scotia",
"qc"=>"Quebec",
"on"=>"Ontario",
"mb"=>"Manitoba",
"sk"=>"Saskatchewan",
"ab"=>"Alberta",
"bc"=>"British Columbia",
"nt"=>"Northwest Territories");?>
So I did that:
<select name = "province[]" multiple size = "12" <?if ($_SERVER['REQUEST_METHOD'] == 'POST'){if (isset($errorList['province']))
{
echo "class=\"error\"";
}}?>>
<?php foreach($PROVINCES as $key => $value) { ?>
<option value="<?php echo $key ?>"<?= (in_array($key, $_POST['province'] ) )?'selected':'';?>><?php echo $value?></option>
<?php }?>
</select>
Now the next step was to make a table of all the values in $_POST
but the problem is since the values are nf, pe, nb etc it will write those in the table and not PrinceEdwardIsland, New Brunswick, Nova Scotia.
echo '<table border="1" style="width:100%">';
foreach($_POST as $name => $out)
{
echo '<tr>';
echo '<td>';
echo '<strong>';
echo strtoupper($name);
echo '</strong>';
echo '</td>';
echo '<td>';
if (is_array($out))
{
count($_POST);
$arrayOutput = implode(", ", $out);
echo $arrayOutput;
}
else if (strlen($out) <= 0)
{
echo "---None supplied---";
}
else
{
echo $out;
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
But as you can see from here we have multiple different $name from $_POST when we call it in our foreach loop.
And as you can see when I do is_array($out) I implode the array and split it out by ","'s I only need to get the full names for provinces, I had a checkbox for Status and it only has to display what the value in the checkbox was. I'm trying to figure out how I can get the $key of $PROVINCES to replace the imploded values in $_POST['province']
Hopefully I explained it well enough for people to understand.

Line break in PHP

I am trying to create a newline character in my foreach loop. After the URL I am trying to create a newline and then have a "------" between the title and content. After this there is supposed to be a space between the next URL. I have the space but I can't seem to get the line break. When I echo "\t" or "\n" in the loop nothing happens. When I use HTML in the loop it won't work, gives too many spaces.
for($i=0; $i < 4 ;$i++)
{
foreach ($json as $URL)
{
echo $URL ['results'][$i]['url'];
echo "---------------";
foreach ($json as $TITLE)
{
echo $TITLE ['results'][$i]['title'];
echo "--------";
}
foreach ($json as $content)
echo $content['results'][$i]['content'];
echo "---------------- ";
}
}
Is there a function in php besides "\n" or another way of manipulating the HTML to insert the line break?
TIA
There are two things you can do:
1: Use <br> as line break. If you use that, you should also consider using <hr> to insert the horizontal line.
some text<br>
<hr>
some other text<br>
2: Use <pre> and </pre> tags around your text to output preformatted text. You can then use \n and \t to format your text.
<pre>
some text\n
-------\n
some other text\n
</pre>
This might the one you are searching for
for($i=0; $i < 4 ;$i++)
{
foreach ($json as $URL)
{
echo $URL ['results'][$i]['url'];
echo "<br/>";
echo "---------------";
foreach ($json as $TITLE)
{
echo $TITLE ['results'][$i]['title'];
echo "--------";
}
foreach ($json as $content)
echo $content['results'][$i]['content'];
echo "---------------- ";
}
}
\n doesn't work in HTML. If you view source of the document you will however see the line breaks. Use html markup like
Test line break<br />
<p>paragraph</p>
<p>paragraph</p>
Simply add <br /> to echo statements. as in html we use this for line breaks.
echo "Content"."<br />";
This would give line breaks in HTML format.
use
echo "<br />";
instead of
echo "---------------- ";
Use echo '';
<?php
echo "first line.<br />Second line.';
?>
or nl2br() function,
<?php
echo nl2br("first line.\nSecond line.");
?>
You can consider
echo "<hr /> ";

How to print php code in HTML tags?

Hello i'm having this variable which is string.
$this->product_output_html = <<< HTML
Some HTML Code
HTML;
I want int he class test to add a php for loop like this one
if ($admin->errors) { foreach ($admin->errors as $error) {
echo ''.$error.''; } }
i have tried to add but is not working. i added '' after the class="test"> and before the of the test but still is not working. what i'm doing wrong?
thanks a lot
try something like
$this->product_output_html = 'Start of html code as a string';
if ($admin->errors) {
foreach ($admin->errors as $error) {
$this->product_output_html .= '<br />'.$error;
}
}
$this->product_output_html .= '<br />End of html code as a string';
echo $this->product_output_html;
replace
echo ".$error.";
with
echo $error;

How do I print all POST results when a form is submitted? [duplicate]

This question already has answers here:
Print out post values
(11 answers)
Closed 6 years ago.
I need to see all of the POST results that are submitted to the server for testing.
What would be an example of how I can create a new file to submit to that will echo out all of the fields which were submitted with that form?
It's dynamic, so some fields may have a name/ID of field1, field2, field3, etc.
All the values are stored in the $_POST collection
<?php print_r($_POST); ?>
or if you want something fancier that is easier to read use a foreach loop to loop through the $_POST collection and print the values.
<table>
<?php
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
?>
</table>
You could try var_dump:
var_dump($_POST)
Simply:
<?php
print_r($_POST);
//Or:
foreach ($_POST as $key => $value)
echo $key.'='.$value.'<br />';
?>
You may mean something like this:
<?php
$output = var_export($_POST, true);
error_log($output, 0, "/path/to/file.log");
?>
You could use something as simple as this
<?php
print_r($_POST);
?>
This would make it a bit more viewable:
<?php
echo str_replace(' ', ' ', nl2br(print_r($_POST, true)));
?>
You can definitely use var_dump, but you mentioned you are in front-end development. I am sure you would know this, but just as a reminder, use Firefox's Firebug or Chrome's / Internet Explorer's developers tool and check for the post. Post goes through hearders, and you should be able to check it from there too.
if (! function_exists('d'))
{
// Debugger
function d($var, $exit = 0)
{
// Only output on localhost
if ($_SERVER['HTTP_HOST'] != 'localhost')
{
return;
}
echo "\n[degug_output_BEGIN]<pre>\n";
echo var_export($var, 1);
echo "\n</pre>[degug_output_END]\n";
if ($exit)
exit;
}
}
// Call:
d($_POST);
Bonus: Check debug_backtrace() too add tracing to your debugging.

Categories