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

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);
}

Related

On submit i need to pass anchor tag value in php

while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo

PHP - How can I retrieve data from a database and display it in a form and make changes to it and update it?

I'm new to PHP and I've been trying out PHP for a while. I have been able to retrieve data from my database and viewed it in a form but I cant seem to update it when I try to change any of the values. My code is below. Ive been trying a few things so sorry for the bad code
<form action="user.php" method="POST">
<h4>Edit Account</h4>
<input type="text" name="editstudent_number" placeholder="Enter Student Number">
<input type="submit" name="editaccount" value="Search"><br>
<?php
if (isset($_POST['editaccount'])){
$GLOBALS['editstudent_number'] = $_POST['editstudent_number'];
$editstudent = $GLOBALS['editstudent_number'];
$edit_sql = "SELECT username, student_number, email, password, progress, rank FROM users WHERE student_number = '$editstudent'";
$edit_query = mysqli_query($conn, $edit_sql);
$edit_fetch = mysqli_fetch_assoc($edit_query);
$username = $edit_fetch['username'];
$student_number = $edit_fetch['student_number'];
$email = $edit_fetch['email'];
$password = $edit_fetch['password'];
$progress = $edit_fetch['progress'];
$rank = $edit_fetch['rank'];
echo "<input type='text' name='username' value='$username' /><br>";
echo "<input type='text' name='student_number' value='$student_number' /><br>";
echo "<input type='text' name='email' value='$email' /><br>";
echo "<input type='text' name='password' value='$password' /><br>";
echo "<input type='text' name='progress' value='$progress' /><br>";
echo "<input type='text' name='rank' value='$rank' />";
echo "<input type='submit' name='editaccount' value='Save changes' />";
}
$GLOBALS['username'] = $_POST['username'];
$GLOBALS['student_number'] = $_POST['student_number'];
$GLOBALS['email'] = $_POST['email'];
$GLOBALS['password'] = $_POST['password'];
$GLOBALS['progress'] = $_POST['progress'];
$GLOBALS['rank'] = $_POST['rank'];
$edit_username = $GLOBALS['username'];
$edit_student_number = $GLOBALS['student_number'];
$edit_email = $GLOBALS['email'];
$edit_password = $GLOBALS['password'];
$edit_progress = $GLOBALS['progress'];
$edit_rank = $GLOBALS['rank'];
if (isset($_POST['editaccount'])){
$sql = "UPDATE users SET username='$edit_username', student_number='$edit_student_number', email='$edit_email', password='$edit_password', progress='$edit_progress', rank='$edit_rank' WHERE student_number = '$editstudent'";
$query = mysqli_query($conn, $sql);
}
?>
</form>
Your Search button and Save change button have same name, which might cause the conflict.
Search
<input type="submit" name="editaccount" value="Search"><br>
Save
<input type='submit' name='editaccount' value='Save changes' />
And looks at how you have SAME if (isset($_POST['editaccount'])){ two times in the codes.
Change the names.
And also, use different FORM for the Search and the Update.
This is your codes that I have cleaned up a bit, and hopefully working.
<h4>Edit Account</h4>
<!-- search form -->
<form action="user.php" method="POST">
<input type="text" name="editstudent_number" placeholder="Enter Student Number">
<input type="submit" name="searchstudent" value="Search"><br>
</form>
<!-- update form -->
<form action="user.php" method="POST">
<?php
if (isset($_POST['editaccount']))
{
$edit_username = $_POST['username'];
$edit_student_number = $_POST['student_number'];
$edit_email = $_POST['email'];
$edit_password = $_POST['password'];
$edit_progress =$_POST['progress'];
$edit_rank = $_POST['rank'];
$sql = "UPDATE users SET username='$edit_username', student_number='$edit_student_number', email='$edit_email', password='$edit_password', progress='$edit_progress', rank='$edit_rank' WHERE student_number = '$edit_student_number'";
$query = mysqli_query($conn, $sql);
}
if (isset($_POST['searchstudent']))
{
$editstudent = $_POST['editstudent_number'];
$edit_sql = "SELECT username, student_number, email, password, progress, rank FROM users WHERE student_number = '$editstudent'";
$edit_query = mysqli_query($conn, $edit_sql);
$edit_fetch = mysqli_fetch_assoc($edit_query);
$username = $edit_fetch['username'];
$student_number = $edit_fetch['student_number'];
$email = $edit_fetch['email'];
$password = $edit_fetch['password'];
$progress = $edit_fetch['progress'];
$rank = $edit_fetch['rank'];
echo "<input type='text' name='username' value='$username' /><br>";
echo "<input type='text' name='student_number' value='$student_number' /><br>";
echo "<input type='text' name='email' value='$email' /><br>";
echo "<input type='text' name='password' value='$password' /><br>";
echo "<input type='text' name='progress' value='$progress' /><br>";
echo "<input type='text' name='rank' value='$rank' />";
echo "<input type='submit' name='editaccount' value='Save changes' />";
}
else
{
echo "Please search the student number to update the details.";
}
?>
</form>
Just a note, you SHOULD NOT allow user to UPDATE the PRIMARY KEY of your table. In your case, the Student Number is the primary keys, yet you allow user to update it. This will causes conflicts and errors in the update process.

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.

HTML Form sending an Array

I have a form that looks like the following:
<form action="results.php" method="get">
<input type='checkbox' name='batch[]' value='1'>
<input type='text' name='job_id[]' value='111'>
<br>
<input type='checkbox' name='batch[]' value='1'>
<input type='text' name='job_id[]' value='999'>
</br>
<input type='submit' name='submit' value='Submit'>
</form>
In the example below I have only selected the row with 999 in the textbook.
The results are displayed in the results.php page which code looks like this:
<?php
$batch = $_GET['batch'];
$job_id = $_GET['job_id'];
foreach($job_id as $key => $value) {
echo $batch[$key]." ";
echo $value."<br>";
}
?>
The above code displays like this:
1 111
999
As you can see the 1 (checkbox) is next to 111. I want to be able to allow send across the job_id from the row selected.
Hopefully I have explained the problem well enough.
Many thanks,
John
add index number in html :
<form action="viewport.php" method="get">
<input type='checkbox' name='batch[1]' value='1'>
<input type='text' name='job_id[1]' value='111'>
<br>
<input type='checkbox' name='batch[2]' value='1'>
<input type='text' name='job_id[2]' value='999'>
</br>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
if(isset($_GET['batch'])) {
$batch = $_GET['batch'];
$job_id = $_GET['job_id'];
foreach($job_id as $key => $value) {
if(isset($batch[$key])) {
echo $batch[$key]." ";
echo $value."<br>";
}
}
}
?>
It will only print:
1 999
because only second check-box is checked.
Give textbox value to checkbox. Same value set to checkbox and textbox.
Javascript
<form action="test5.php" method="get">
<input type='checkbox' name='batch[]' value='111'>
<input type='text' name='job_id[]' value='111'>
<br>
<input type='checkbox' name='batch[]' value='999'>
<input type='text' name='job_id[]' value='999'>
</br>
<input type='submit' name='submit' value='Submit'>
</form>
PHP
<?php
$batch = $_GET['batch'];
$job_id = $_GET['job_id'];
foreach($batch as $key => $value) {
echo $value."<br>";
}
?>

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

Categories