PHP variable as input value - php

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.

Related

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>";

Having trouble nesting with three levels of quotes in 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.

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.

Ask if you are sure you want to submit? JavaScript?

I currently have this.
//other stuff up here - not important
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' /><input type='submit' name='Redeem' value='Redeem'></form></td>";
} else {
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' /><input type='submit' name='Un-Redeem' value='Un-Redeem'></form></td>";
//other stuff down here - not important
and I want to change it so that when you press:
a) the 'Redeem' SUBMIT button it alerts you saying, "Are you sure you want to Redeem?"
b) the 'Un-Redeem' SUBMIT button it alerts you saying, "Are you sure you want to Un-Redeem?"
I have tried a few of them including the ONCLICK on the submit but none worked.. and I believe that is because I have it in an ECHO and I had to remove the (") quotation marks which stopped the function from happening.
Anyone know another way I can do it?
You can use the Javascript confirm function.
if(confirm("Are you sure you want to Redeem?")) {
// do something
} else {
// do something
}
You can also do this on form submit by adding the following code to your form:
onsubmit="return confirm('Are you sure you want to Redeem?');"
The form will only submit if the user clicks "OK".
Here is the solution :
//other stuff up here - not important
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Redeem' value='Redeem' onclick="return confirm('Are you sure you want to Redeem?')"></form></td>";
} else {
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Un-Redeem' value='Un-Redeem' onclick="return confirm('Are you sure you want to Un-Redeem?')" ></form></td>";
//other stuff down here - not important
Edit:
Added escaping character in here:
//other stuff up here - not important
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='1' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Redeem' value='Redeem' onclick=\"return confirm('Are you sure you want to Redeem?')\"></form></td>";
} else {
echo "<td><form action='redeem.php' method='post' id='form'><input type='hidden' name='redeem' value='0' /><input type='hidden' name='id' value='" . $id . "' />
<input type='submit' name='Un-Redeem' value='Un-Redeem' onclick=\"return confirm('Are you sure you want to Un-Redeem?')\" ></form></td>";
//other stuff down here - not important
Use the javascript confirm function. The form won't submit if the confirm returns false.
<form action='redeem.php' method='post' id='form' onSubmit="return confirm('Are you sure you want to redeem')">
If you want to do something else if the user clicks cancel then you'll need to create a custom function:
function my_confirm() {
if( confirm("Are you sure you want to redeem?") ) {
return true;
}
else {
// do something
return false;
}
}
And on the form tag:
onSubmit="return my_confirm()"

Categories