How to pass session variable into a form in PHP? - 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>";

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 } ?>

Defining dynamic Form ID in PHP

I want to define dynamic form id for the following example form.
The following code doesn't give me the result I want. for example, instead of <form id="form1">,
I get this result : <form id='form''1'>
How should I write it so it gives me the result I want? Thanks !!
<?php
while ($data = mysqli_fetch_array($subject)) {
echo "<form id='form'.'{$data['productID']}' method='post' action=' ' data-checktable='{$data['productID']}'>";
echo "<input type='checkbox' class='SelectAll'>All";
echo "<label><input type='checkbox' class='selector' value='{$data['product1']}'>" . $data['product1']."</label>";
echo "<label><input type='checkbox' class='selector' value='{$data['product2']}'>" . $data['product2']."</label>";
echo "</form>";
}
?>
You just need to Change it to
echo "<form id='form{$data['productID']}' method='post' action=' ' data-checktable='{$data['productID']}'>";
i.e. remove the extra '.' between the word form and the ID you wanted.

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