Getting a value by type="hidden" - php

In my application I'm printing out searchresults. I want to use those searchresults to send an invite to become a member in that group. I'm printing out those userdata but I'm wondering how I can use those data into another function (sendInvite or something). I have to get the user_id of that people ($row['user_id'] but I don't know how I can get it. I'm thinking about type="hidden" but I never used it before.)
PRINT OUT
<div id="InviteGroupMembers">
<form action="<?php echo $_SERVER['PHP_SELF'] . "?group_id=" .$group_id; ?>" method="post">
<div >
<input type="text" name="btnSearch" placeholder="Add people to group"/>
</div>
<div>
<button type="submit" name="">Add</button>
</div>
</form>
<form action="<?php echo $_SERVER['PHP_SELF'] . "?group_id=" .$group_id; ?>" method="post">
<?php
if (is_object($searchresult))
while ($row = $searchresult -> fetch_array()) {
echo "<div><p class='searchresults'><img src='uploads/" . $row['avatar'] . " " . "' alt='' />" . $row['surname'] . " " . $row['name'] . " " . "<input type='submit' name='btnAddMember'class='addFriendToGroup' value='' /><input type='hidden' name='user_id' value='" . $row['user_id'] . "'/></p></div>"; }
?>
</form>
</div>
PHP
if (isset($_POST["btnSearch"])) {
try {
$searchinput = mysql_real_escape_string($_POST['btnSearch']);
$searchresult = $user -> Search($searchinput);
} catch(exception $e) {
$feedback = $e -> getMessage("no results");
}
}
if (isset($_POST["btnAddMember"])) {
try{
$group_receiver_id = mysql_real_escape_string($_POST['user_id']);
var_dump($group_receiver_id);
}
catch(exception $e) {
$feedback = $e -> getMessage("no results");
}
}

input type hidden would work for you in this instance. The output below is based on it being within php code. Other wise you will need to use php tags within the value attribute.
<input type="hidden" name="user_id" value="$row['user_id']"/>
Then you can just get the post variable on form submission

Related

Insert php sql row record into hidden form input value field

I'm trying to insert sql ID record inside php echo form input value. Values that are inserted manualy via input field (by typing) are being displayed fine. Value inside input (named: potnik) is not being displayed. Is it ignored because is inside echo or is inserted wrong?
$sql3 = "
SELECT id, potnik_id, ura, naslov
FROM prevoznik
ORDER BY HOUR(ura), MINUTE(ura) ASC;
";
$result = $conn->query($sql3);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Spremenjena oblika datuma
$date = date_create($row["ura"]);
$ura_pobiranja = date_format($date,"H:i");
echo "<div class=\"row list divider-gray\">
<div class=\"col-1 fs-09 fw-600\">" . $row["id"] . " </div>
<div class=\"col-3 flex-vcenter-items fw-600 fs-09\">" . $row["naslov"] . " </div>
<div class=\"col-1 flex-vcenter-items fw-600 fs-09\">$ura_pobiranja</div>
";
if ($row["naslov"] !== null) {
echo " <div class=\"col-6 flex-vcenter-items fs-1\">Nastavi uro<form action='update.php?id=" . $row["id"] . "\"' method='POST'><input name=\"potnik\" value='".$row["id"]."' type='hidden' /> <input class=\"form-control fancy-border\" type=\"text\" name=\"posodobljeni_cas\"/><input type='submit' value='Posodobi'> </form></div>";
echo " </div>";
}
else {
echo " </div>";
}
}
} else {
echo "<div class=\"col flex-vcenter-items fw-100 fs-1\"><i class=\"far fa-frown-open pr-3\"></i>Nimaš še nobenih opravil
</div>";
}
Code that isn't working (hidden input field value):
echo " <form action='update.php?id=" . $row["id"] . "\"' method='POST'><input name=\"potnik\" value='".$row["id"]."' type='hidden' /> <input class=\"form-control fancy-border\" type=\"text\" name=\"posodobljeni_cas\"/><input type='submit' value='Posodobi'> </form></div>";
I had post parameters instead of get parameters set for update.php form action
update.php?id=
echo"Error on update ID:{$_GET['id']} POSODOBLJENI CAS:{$_POST['posodobljeni_cas']}";

When I search something in search box , the keyword which I entered disappears after giving the result

I have an ideal search-box which searches item from a given listing. My searching is working fine, the problem is when I enter something in search box to search, it gives me result but my search box gets empty.
For eg If I search "Electronics" in search box, it gives me result of electronics but my search box gets empty. It should be written with "Electronic" when it gives me result.
Probably, I should be using GET method instead is it so?
Here is my code for searching:
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM category WHERE cat_name LIKE '%" . $term . "%' or parent LIKE '%" . $term . "' or cat_status LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
$dynamicList="";
while ($row = mysql_fetch_array($r_query))
{
// $cat_id=;
/*$dynamicList .= '
<img style="border:#666 1px solid;" src="../storeadmin/category/thumbs/' . $row['cat_id'] . '.jpg" width="77" />';*/
echo "<tr bgcolor=''>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "<td><img style='border:#666 1px solid;' width='70' src='http://localhost/jaymin/My%20Store/storeadmin/category/thumbs/". $row['cat_id'].".jpg' /></td>";
//echo "<td>".$dynamicList."</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['parent'] . "</td>";
echo "<td>" . $row['cat_status'] . "</td>";
echo "<td><a href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
change your code to
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
instead of
Search: <input type="text" name="term" /><br />
Try this,
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
If you want this in your url use GET instead of POST like,
<form action="" method="get">
Search: <input type="text" name="term" value="<?php if(isset($_GET['term'])){ echo $_GET['term']; } ?>" /><br />
<input type="submit" value="Submit" />
</form>
This is because you are submitting the form and after form submit textbox values disappears. To overcome this try:
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>

How to get value from input with data from database

I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}

Add a 'name' to a Paragraph tag to work with POST

I'm working on a php project where I want to receive a _POST of a paragraph in my form.
What's the best way to do that ?
I thought the easiest way to do that was adding a 'name' to the tag but that seems not to work.
<form action="Index.php" method=post enctype="multipart/form-data">
<?php
foreach($optevragenDataVanWerknemer as $info)
{
echo "<p name = 'wnr'><strong>".$info->wnr."</strong></p> </br>";
echo "<p> Afdeling : " . $info->afdeling . "</p>";
echo "<p> Functie : " . $info->ftienaam . "</p>";
echo "<p> Salaris : " . $info->salaris . "</p>";
echo "<p> Vesnaam : " . $info->vesnaam . "</p>";
echo "<p><img src='" . Config::getConfigInstantie()->getUploadMap() . '/' . $info->foto . "' alt=foto width=45 height=30></p>";
?>
<p>
<input type=hidden name="MAX_FILE_SIZE" value=1000000>
<label for=foto>Nieuwe foto </label>
<input type=file name=foto id=foto>
<input type=submit name=uploadKnop value="Upload Foto">
</p>
<?php
}
?>
$wnr = $_POST["wnr"]);
You can get only form elements (Form Elements) in the action page ie from $_POST. You can better assign the same value to a hidden element and access it.
<form action="Index.php" method=post enctype="multipart/form-data">
<?php
foreach($optevragenDataVanWerknemer as $info)
{
echo "<p><strong>".$info->wnr."</strong></p> </br>";
echo "<input type='hidden' name = 'wnr' value='{$info->wnr}'> </br>";
echo "<p> Afdeling : " . $info->afdeling . "</p>";
echo "<p> Functie : " . $info->ftienaam . "</p>";
echo "<p> Salaris : " . $info->salaris . "</p>";
echo "<p> Vesnaam : " . $info->vesnaam . "</p>";
echo "<p><img src='" . Config::getConfigInstantie()->getUploadMap() . '/' . $info->foto . "' alt=foto width=45 height=30></p>";
?>
<p>
<input type=hidden name="MAX_FILE_SIZE" value=1000000>
<label for=foto>Nieuwe foto </label>
<input type=file name=foto id=foto>
<input type=submit name=uploadKnop value="Upload Foto">
</p>
<?php
}
?>
</form>
You need to have data you want in $_POST to be within a form element. A <p> tag will never submit data. Try <textarea> or <input>
foreach($optevragenDataVanWerknemer as $info) {
echo "<textarea name = 'wnr'>" . $info->wnr . "\n";
echo "Afdeling : " . $info->afdeling . "\n";
echo "Functie : " . $info->ftienaam . "\n";
echo "Salaris : " . $info->salaris . "\n";
echo "Vesnaam : " . $info->vesnaam . "</textarea>";
}
I took out your <img> tag because that's an entirely different process to upload. If you're trying to store the raw HTML you could add it back into the <textarea> and it would submit as text.

php update dynamic sql rows through form

My goal is to retrieve a dynamic SQL list of rows and edit every item in the list within the same page using POST (no multiple changes at once required). Can this be achieved within the same page? I made the script below which displays everything the way i want to, however the switch statement doesn't seem to be fit for purpouse.
I am aware this code might not be best practice (not to mention secure). Sorry for being inflexible, but i'm not allowed to use anything other then PHP/HTML
Code:
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["voornaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" style="width:500px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
}
} else {
echo "0 resultaten";
}
switch(!empty($_POST))
{
case !empty($_POST[$row["voornaam"]]):
// update SQL query 'firstname' here
break;
}
$conn->close();
?>
By this way you actualy see what you are editing and it's more clean.
// PDO DB conecction (secure way)
try {
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $exception){ //to handle connection error
echo "Connection error: " . $exception->getMessage();
}
if isset($_POST["voornaam"]){
$stmt = $con->prepare(" QUERY");
$stmt->execute();
echo "YEAH you did it";
} else {
stmt = $con->prepare(" QUERY");
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $rs)
{
$voornaam = $rs["voornaam"]
$achternaam = $rs["achternaam"]
$omschrijving = $rs["omschrijving"]
}
}
// Just an example
echo '<form action= "" method="post"> <input type="text" name="voornaam" value="'. $voornaam .'" style="width:100px"><input name="submit" type ="submit" value="Submit"><br/><br/>';
Value is also the input so you can have only one Submit button at the end cause all other data will be overwritten by itself.
<?php
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo '<tr> <td class="tg">' .$row["id"]. '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["voornaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["achternaam"].'" style="width:100px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td>' . "\n";
echo '<td class="tg"> <form action= "" method="post"> <input type="text" name="voornaam" value="'.$row["omschrijving"].'" style="width:500px"> <input name="submit" type ="submit" value="Aanpassen"><br/><br/>' . '</td> </tr>' . "\n";
}
}
else
{
echo "0 resultaten";
}
if( isset( $_POST[$row["voornaam"]] ) && !empty( $_POST[$row["voornaam"]] ) )
{
// update SQL query 'firstname' here
}
$conn->close();
?>
In your switch you're sending !empty($_POST) so boolean (true or false) so unique cases would be: True or False...
It's better if you change your switch to:
if(isset($_POST[$row["voornaam"]]) && !empty($_POST[$row["voornaam"]])){
// update SQL query 'firstname' here
}

Categories