Having trouble nesting with three levels of quotes in PHP - php

Please forgive me if I've posted this incorrectly, or there's an answer to this question somewhere - I have been searching for an answer to this but I haven't found one.
I'm nesting quotes within an echo statement and it's giving me grief (a 500 Error)
This code works (and various arrangements of double and single quotes):
<form action='update.php' method='post'>
<p>
<input type='hidden' name='log_id' value='<?php echo $row["log_id"]; ?>'/>
<input type='time' name='time_back' id='time_back'>
<input type='submit' value='Update'>
</p>
</form>
This code does not seem to work:
<?php
if($row["time_back"] == '00:00:00')
{ echo "<form action='update.php' method='post'>
<p>
<input type='hidden' name='log_id' value='<?php echo $row[\"log_id\"]; ?>'/>
<input type='time' name='time_back' id='time_back'>
<input type='submit' value='Update'>
</p>
</form>";}
else { echo $row["time_back"]; }
?>
I just can't quite see what I'm getting wrong here. I've poured over all sorts of stuff on nested quotes and I still can't figure it out.

The best thing to do (if you're not going to use a templating engine) is stop printing so much html with echo. I.e. close and reopen the PHP tag to make most of the HTML printed normally by being outside of PHP tags:
<?php
if($row["time_back"] == '00:00:00')
{
?>
<form action='update.php' method='post'>
<p>
<input type='hidden' name='log_id' value='<?php echo $row['log_id']; ?>' />
<input type='time' name='time_back' id='time_back'>
<input type='submit' value='Update'>
</p>
</form>
<?php
}
else { echo $row["time_back"]; }
?>
Second best, Close the quotes and concatenate, then reopen them:
<?php
if($row["time_back"] == '00:00:00')
{ echo "<form action='update.php' method='post'>
<p>
<input type='hidden' name='log_id' value='" . $row['log_id'] . "' />
<input type='time' name='time_back' id='time_back'>
<input type='submit' value='Update'>
</p>
</form>";
}
else { echo $row["time_back"]; }
?>

In your desired format
<?php
if($row["time_back"] == '00:00:00')
{ echo "<form action='update.php' method='post'>
<p>
<input type='hidden' name='log_id' value='<?php echo $row[\"log_id\"]; ?>'/>
<input type='time' name='time_back' id='time_back'>
<input type='submit' value='Update'>
</p>
</form>";}
else { echo $row["time_back"]; }
?>
You have a nested PHP statement - you're still working within the one you declared at the start, and are actually inside an 'echo' function that has not finished. There are many ways of merging your HTML with PHP, but one of the most readable
is the concatenation operator ..
So, with this method, the correction would be that in this line
<input type='hidden' name='log_id' value='<?php echo $row[\"log_id\"]; ?>'/>
You would instead write
<input type='hidden' name='log_id' value='" . $row["log_id"] . "'/>
Notice that we close the opening quote ", concatenate, output our PHP (we don't need the PHP tag or echo because we're already in an echo function, we just need to concatenate the string we want to be merged into the HTML), then concatenate again and reopen the ".
Hope that helps!

As was mentioned in comments, don't open a new PHP tag (i.e. <?php). Instead, just end the string literal (i.e. terminate with ") and concatenate the value from the variable (i.e. the array element: $row['log_id']).
<?php
if($row["time_back"] == '00:00:00')
{ echo "<form action='update.php' method='post'>
<p>
<input type='hidden' name='log_id' value='". $row["log_id"] ."'/>
<input type='time' name='time_back' id='time_back'>
<input type='submit' value='Update'>
</p>
</form>";}
else { echo $row["time_back"]; }
See it demonstrated in this playground example.

Related

change to php 8 some POST data after HTML form submit is empty [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 days ago.
I pass data from a form (once hidden and once by input) by click to a function. But unfortunately the data on the hidden input field is given to the $_POST
<?php>
$abschlag1 = "gelb";
$ndbw01 = 8;
echo "<form name='Formular' action='' method='post' >";
echo "<td align=center><input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2'>" . $ndbw01 . "</td>";
echo "<input type='hidden' id='abschlag1' name='abschlag1' value=$abschlag1>";
echo "<input type='submit' name='submit1' value='Speichern' onclick='chkFormular();'/>";
echo "</form>";
?>
<script type="text/javascript">
function chkFormular() {
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
}
</script>
The result ist:
$_POST['abschlag1‘] => gelb
$_POST['L01w‘] => empty
I hope, it is now a little clearer.
You can't run a PHP script on a JavaScript event directly. PHP usually is executed at page generation on the server side but if really needed you can run a PHP script on a JavaScript event using Ajax for example.
In order to help you, correct me if I'm wrong, but I think what you want to do is having your fields still completed with the sent values after the form is submitted. You can do it like this using:
value='<?php if isset($_POST["field"]) echo $_POST["field"]; ?>'
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?php if isset($_POST['L01w']) echo $_POST['L01w']; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?php if isset($_POST['abschlag1']) echo $_POST['abschlag1']; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>
If you still want to keep your data in variables, you update your code to this:
<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
<form name='Formular' action='' method='post'>
<td align='center'>
<input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?= $L01w; ?>' /><?= $ndbw01; ?>
</td>
<input type='hidden' id='abschlag1' name='abschlag1' value='<?= $abschlag1; ?>' />
<input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>

How can I use PHP variables inside HTML tags?

I have the following doubt. I want to use php code to create a HTML form which outputs three values. And I want to use that values on a php function, something alike the following:
<?php
function modcreate(){
echo "<input type='text' name='dato'>";
echo "<input type='text' name='nvalor'>";
echo "<input type='text' name='identif'>";
echo "<input type='button' onclick=mod($GET['dato'], $GET['nvalor'],
$GET['identif']>";
}
?>
Any suggestions or cleaner ways to do this? Thank you beforehand
Try this one
<?php
function modcreate(){
echo "<input type='text' name='dato'>";
echo "<input type='text' name='nvalor'>";
echo "<input type='text' name='identif'>";
echo "<input type='button' onclick=" . mod($GET['dato'], $GET['nvalor'],
$GET['identif'] . ">";
}
?>
You were actually inserting php code inside double quotes which was behaving like a simple string. So close your double quotes before adding php tag
you dont need to echo html in php
function modcreate(){
?>
<input type='text' name='dato'>
<input type='text' name='nvalor'>
<input type='text' name='identif'>
<input type='button' onclick="mod(<?php (implode(',',[$GET['dato'],$GET['nvalor'],$GET['identif']);?>)">;
<?php
}
For a better syntax of how to handle html without echo is:
<?php
function modcreate(){ ?>
<input type='text' name='dato'>
<input type='text' name='nvalor'>
<input type='text' name='identif'>
<input type='button'
onclick="mod(
<?php (implode(',',[$GET['dato'],$GET['nvalor'],$GET['identif']); ?>
)">
<?php } ?>

How to pass session variable into a form in PHP?

I currently have this code but it is not display the u_mail
if (isset($_SESSION['u_email'])) {
echo "<form method='POST' action='".setComments($connComment)."'>
<input type='hidden' name='uid' value='$_SESSION['u_email']'>
</form>" }
getComments($conn)
How do I display it?
I tried testing it by doing:
<?php
echo $_SESSION['u_email'];
?>
And it works completely fine. It is just when I put it in an echo. I actually SHOULD put it in echo. Is there a way in PHP to do that?
Thank you!
Just break out from the string and concatenate, like this:
echo "<form method='POST' action='".setComments($connComment)."'>
<input type='hidden' name='uid' value='" . $_SESSION['u_email'] ."'>
</form>";

How to use string from condition1 into condition2?

I have the following code:
<form action='' method='POST' id='form1'>
imdbcode : <input type='text' id='imdbcode' name='imdbcode' /><br/>
<input type='submit' name='submit' value='Get'/>
</form>
<?php
if(isset($_POST['submit'])){
.
.
.
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
//I WANT TO USE $Title from condition1
} ?>
I want to use $title in second condition.
It prints $title in first condition! But doesn't print after closing condition!
How can I do that?
You can't.
Before you can enter the if(isset($_POST['Send']))
you need submit this one:
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
but the moment you submit this, the page will be refreshed and $_POST['submit'] will be deleted, without this variable the $title will not exist.
to fix this or make the $title value alive until the next refresh of the page. you must include the $title value along with the form2.
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value="$title"/>
<input type='submit' name='Send' value='Send'/>
</form>";
and when the user send that form2 you can access the title by using
$_POST['title']
As two your forms are different - there's no connection between them unless you explicitly provide it.
Simple solution can be just echo your $title as a hidden field in a form:
if(isset($_POST['submit'])){
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value='" . $title . "'/>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_POST['title'];
// do other stuff
}
Another solution is to use sessions:
if(isset($_POST['submit'])){
$title = ... ;
$_SESSION['title'] = $title;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_SESSION['title'];
// do other stuff
}
In case of using sessions don't forget to use session_start and probably to unset $_SESSION['title'] in the end of second submit.
<form action='' method='POST' id='form2'>
<input type='hidden' name='text' value='<?php echo $title; ?>
<input type='submit' name='Send' value='Send'/>
</form>
Try to add a hidden input before the Send button.

PHP variable as input value

I'm trying to make a voting system. The form below is located in an echo. So I want the $entree_id to be filled in as the value of the hidden field and stored in a database when someone clicks on the submit button. But whenever I try it, the row stays empty.
This is the database output:
$entree_id = $row['entree_id'];
And this is in the echo:
<form action='/' method='post'><input type='hidden' name='entree_id' value='" . $entree_id . "'><input type='submit' class='button'></form><p>
I have also tried something like this:
<form action='/' method='post'><input type='hidden' name='entree_id' value='<?php print $entree_id ?>'><input type='submit' class='button'></form><p>
Why don't I get the value of $entree_id?
you must distinguish between html and php. your variables should be betwen php tags.
try this
<form action='' method='post'><input type='hidden' name='entree_id' value='<?php echo $entree_id ; ?>' /><input type='submit' class='button' /></form><p>
EDIT:
inside the echo then it will be like this
echo "<form action='' method='post'>
<input type='hidden' name='entree_id' value='".$entree_id."' />
<input type='submit' class='button' />
</form>" ;
make sure that type is hidden .
change it to
type='text' //or what ever to see the value.
Both of those SHOULD work if there is already a value in $entree_id at the time PHP is running. If you do the first one you were talking about...
<form action='/' method='post'><input type='hidden' name='entree_id' value='" . $entree_id . "'><input type='submit' class='button'></form><p>
... then you will need it to be part of an echo statement (which it sounds like you might already be doing):
<?php
$entree_id = 5;
echo "<form action='/' method='post'><input type='hidden' name='entree_id' value='" . $entree_id . "'><input type='submit' class='button'></form><p>";
?>
(Don't forget the semicolon at the end!) This produced output for me like this:
<form action='/' method='post'><input type='hidden' name='entree_id' value='5'><input type='submit' class='button'></form><p>
But you could also do it the 2nd way you were saying, I think this might be more readable. You don't have to use an echo statement for the whole thing, just for the variable:
<?php
$entree_id = 5;
?>
<form action='/' method='post'><input type='hidden' name='entree_id' value='<?php echo $entree_id; ?>'><input type='submit' class='button'></form><p>
Notice I changed your "print" to an "echo" and I also added a semicolon at the end of the echo.
This worked for me too, producing this output:
<form action='/' method='post'><input type='hidden' name='entree_id' value='5'><input type='submit' class='button'></form><p>
Hope this helps! Let me know if you have any other questions.
Change your echo to use single quotes, instead of double quotes, like so:
echo '
<form action="/" method="post"><input type="hidden" name="entree_id" value="' . $entree_id . '"><input type="submit" class="button"></form>';
double quotes can be used in php for outputting variables automatically. Using Single quotes makes this less confusing and ensures that the . is concenating to it properly.

Categories