How to use string from condition1 into condition2? - php

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.

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>

is it possible to add a variable inside the isset $_post['submit']

Is it possible to do something like this?
if (isset($_POST['Submit_'.$_POST['ID']])) {
}
or
if (isset($_POST['Submit_' + $_POST['ID']])) {
}
I want to do this so i can get a different submit button for all the posts, because i got a comment system, inside a posts system.. so on all the posts its a new submit button.
submitbutton is made like this:
echo "<button type='submit' class='commentbtn' name='commentSubmit_".$row['ID']."'></button>
i have checked that all the buttons gets a different name, so its just the other thing i dont get to work.
comment form:
echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
<textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
<input type='hidden' name='uname' value='".$_SESSION['name']."'>
<input type='hidden' name='uid' value='".$_SESSION['id']."'>
<input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
<img src='images/comment-icon.png' height='24'' alt='comment' title='Comment'' />
</button></form></div>";
setComment Function:
function setComment($conn) {
if(isset($_POST['sid']) && isset($_POST['commSubmit_'.$_POST['sid']])){
$uname = $_POST['uname'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$comment = $_POST['commentText'];
$uimg = $_POST['uimg'];
$sid = $_POST['sid'];
$sql = "INSERT INTO status_comments (uid, sid, uname, comment, uimg, date) VALUES ('$uid', '$sid', '$uname', '$comment', '$uimg', '$date')";
$result = mysqli_query($conn, $sql);
}
}
complete setStatus Function with comment form inside:
function getStatus($conn) {
$sql = "SELECT * FROM status ORDER BY sid DESC";
$query = mysqli_query($conn, $sql);
while ($row1 = $query->fetch_assoc()) {
echo "<div class='commentbox'>";
echo "<div class='commentimg'><img src='images/".$row1['commentimg']."' width='50px'></div>";
echo "<div class='namedate'>";
echo $row1['uidname']."<br>";
echo "<div class='commdate'>".$row1['date']."<br><br></div></div><hr>";
echo "<div class='statusmessage'><p>".nl2br($row1['message'])."</p></div>";
echo "<div class='statusimage'><img src='userimages/".$row1['status_image']."'></div>";
echo "<div class='likerow'>";
$result = mysqli_query($conn, "SELECT * FROM status_like WHERE uid=".$_SESSION['id']." and sid=".$row1['sid']."");
if (mysqli_num_rows($result) == 1) {
echo "<span><a href='' class='unlike' id='".$row1['sid']."'><div class='unlike-btn'><img src='images/dislike.png'></div></a><p>| ".$row1['likes']." likes!</p></span>";
} else {
echo "<span><a href='' class='like' id='".$row1['sid']."'><div class='like-btn'><img src='images/like.png'></div></a><p>| ".$row1['likes']." likes!</p></span>";
}
echo "</div>";
echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
<textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
<input type='hidden' name='uname' value='".$_SESSION['name']."'>
<input type='hidden' name='uid' value='".$_SESSION['id']."'>
<input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
<img src='images/comment-icon.png' height='24'' alt='comment' title='Comment'' />
</button></form></div>";
if ($_SESSION['id'] === $row1['uid'] || $_SESSION['usertype'] === 'topadmin'){
echo "<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button type='submit' title='Delete Status' name='statusDelete'>X</button>
</form>
<form class='edit-form' method='POST' action='editcomment.php'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<input type='hidden' name='uid' value='".$row1['uid']."'>
<input type='hidden' name='date' value='".$row1['date']."'>
<input type='hidden' name='message' value='".$row1['message']."'>
<button type='submit' style='border: 0; background: transparent'>
<img src='images/edit-icon.png' height='10'' alt='edit' title='Edit Status'' />
</button>
</form>";
}
echo "</div>";
}
}
Yes your first expression is correct. Check PHP concatenate for more information. Though I would use following to check form submit.
// check if request method is post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// .... your code
}
Additionally, I have had look on your code. I think the error is in form HTML.
echo "<div class='commentform'><form id='comment_form_".$row1['sid']."' action='".setComment($conn)."' method='POST'>
It cause function to run 4 times and recording coment 4 times. Please replace action='#' or action='processComent.php' // location of process code.
Do the same in
<form class='delete-form' method='POST' action='".deleteStatus($conn)."'>
also as same thing will happen while deleting.
Yes, it's possible and your first variant is correct.
Thought a better idea might be make a separate form for each comment field and add the ID as a hidden input.
Something like this:
<form method="post" action="/something">
<textarea name="comment">
<input type="hidden" name="id" value="$row['ID']">
<button type="submit" name="submit"></button>
</form>
I can't really follow the logic in your code, but it seems as if you are over complicating the process... and i assume that your action was not actually pointing to that function as you show, but instead, is pointing to a page with that function ... anyway ... if you have this form
echo "
<div class='commentform'>
<form id='comment_form_".$row1['sid']."' action='setcomment.php' method='POST'>
<textarea name='commentText' class='commenttext' placeholder='Comment this..'></textarea>
<input type='hidden' name='uname' value='".$_SESSION['name']."'>
<input type='hidden' name='uid' value='".$_SESSION['id']."'>
<input type='hidden' name='uimg' value='".$_SESSION['profile_img']."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<input type='hidden' name='sid' value='".$row1['sid']."'>
<button class='commentbtn' name='commSubmit_".$row1['sid']."' type='submit' style='border: 0; background: transparent'>
<img src='images/comment-icon.png' height='24'' alt='comment' title='Comment'' />
</button>
</form>
</div>";
and then when that form posts to setcomment.php, something simple like this would work ...
if(isset($_POST['sid']) && isset($_POST['commSubmit_'.$_POST['sid']])){
setComment($conn);
}
function setComment($conn) {
$uname = $_POST['uname'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$comment = $_POST['commentText'];
$uimg = $_POST['uimg'];
$sid = $_POST['sid'];
$sql = "
INSERT INTO
status_comments
(
uid, sid, uname, comment, uimg, date
)
VALUES
(
'".mysqli_real_escape_string($conn,$uid)."',
'".mysqli_real_escape_string($conn,$sid)."',
'".mysqli_real_escape_string($conn,$uname)."',
'".mysqli_real_escape_string($conn,$comment)."',
'".mysqli_real_escape_string($conn,$uimg)."',
'".mysqli_real_escape_string($conn,$date)."'
)";
$result = mysqli_query($conn, $sql);
}

Loop an array with form

i'm working on some code it seems im stuck... anyone can fix the right source code for looping array form?
<pre>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<form action='' method='POST'>
<?php
if(isset($_POST['jml'])){
$jml = $_POST['jml'];
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name">&nbsp
Stuff Price <input type='text' name='price'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}
if(isset($_POST['submit2'])){
$name[] = $_POST['name'];
$price[] = $_POST['price'];
global $jml;
for($i=0;$i<$jml;++$i){
echo $name.' '.$price.'<br>';
}
}
?>
</pre>
i was basic at C++ it's so easy to loop, but i'm still much to learn on this php so anyone can help me please?
What you need to do first is if you're expecting multiple row fields, put a [] in your name attribute in text fields. Example:
<input type="text" name="name[]">
This in turn will accept multiple text inputs under the same name turning it into an array.
Next, since you're reliant on the number of fields to be generated by $jml = $_POST['jml']; this will just be available on the first request. On the next submission this will be gone. Instead of using that with a global which doesn't make sense, just use the count of the submitted text fields.
$name = $_POST['name'];
$price = $_POST['price'];
$count = count($name); // get count
Take not that this is reliant to the forms all fields being complete.
After that, its just basic array pointing. echo $name[$i]:
Revised code:
<pre>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<form action='' method='POST'>
<?php
if(isset($_POST['jml'])){
$jml = $_POST['jml'];
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name[]">&nbsp
Stuff Price <input type='text' name='price[]'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}
if(isset($_POST['submit2'])){
$name = $_POST['name'];
$price = $_POST['price'];
$count = count($name);
for($i=0;$i<$count;++$i){
echo $name[$i].' '.$price[$i].'<br>';
}
}
?>
</pre>
Use foreach loop This is how you can easily loop through an array:
foreach($_POST as $key=>$value){
echo $value;
}
Check this
Working code here http://main.xfiddle.com/7ffb488b/stackoverflow/Loopanarraywithform.php
<pre>
<?php
if(isset($_POST['submit'])){
echo "<form action='' method='POST'>";
$jml = intval($_POST['jml']);
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name[]">&nbsp
Stuff Price <input type='text' name='price[]'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}elseif(isset($_POST['submit2'])){
$name = $_POST['name'];
$price = $_POST['price'];
// global $jml;
$i=0;
foreach ($name as $key => $value) {
echo $value.' '.$price[$key].'<br/>';
$i++;
}
}else{?>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<?php
}
?>
</pre>

how to use two form's post value together

I have a form on a page like:
<form action='search.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='submit' />
</form>
on search page there is another form like
<form action='' method='POST'>
<input type='submit' name='anygender' />
</form>
then i am using
if(isset($_POST['anygender'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}
then $specialist is showing blank and sql query not working..I want to use both form's post value together.Please tell me how to use first form post value in this. Thanks in advance
Why you need two forms?
<form action='index.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='anygender' />
<input type='submit' name='submit' />
</form>
Maybe you can use one form and check buttom?
Use this code. Check $_POST['submit'] in if condition.
if(isset($_POST['submit'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}

validating dynamic text box content in PHP

I have this and works fine. But can someone tell me is there a SMARTER way to validate this?
In the validation here, you may find that the echo "<form....... is replicated thrice to make sure that the form content is also visible along side the validation message when the form is submitted. Im sure there is a more refined method of validating text box content without form being replicated multiple times.
Also appreciate if some one can suggest me a method to retain the text box value after the form is being submitted. Usually when the form is submitted the value you enter disappears.
Thanks
<?php
include ("../connection/index.php");
?>
<form method='post' action=''>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
}
if(isset($_POST['sendtwo']))
{if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
echo "Empty";}
else
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
echo "Hit success!";
}}
?>
EDIT
Concept
<hard coded submit1 button>
<dynamically create a text box with a submit2 button>
<when submit2 is activated is should validate the text box content>
When the validation happens both the vaidated message and the text box should be visible
did you mean something like this?
<?php
if(isset($_POST['sendtwo']))
{
echo "Hello";
}
?>
<form method='post' action=''>
<?php
if (!isset($_POST['sendone'])) {
?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
<?
} else {
?>
<input type="hidden" name="sendone" id="sendone" value="OneClick">
<input type='text' name='txt1' id='txt1'<?=isset($_POST['txt1']) && trim($_POST['txt1']) !== '' ? ' value="'.trim($_POST['txt1']).'"' : ''?>>
<input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>
<?
}
?>
</form>
<?php
if (isset($_POST['sendtwo'])) {
if (trim($_POST['txt1']) == '') {
echo 'Empty';
} else {
echo 'Hit success!';
}
}

Categories