Reply to a message - php

I have the following code:
<select name="to" class="combo" value='
<?php
if(isset($_POST['reply']))
{
echo "<option value='$reply'>$reply</option>";
}
?>
' />
<?php
$q = $database->selectAllUsersNotMe();
while($row=mysql_fetch_assoc($q))
{
$u=$row['username'];
echo "<option value=\"$u\">$u</option>";
}
?>
</select>
What this does is produce a combo box with a dropdown for all users on my site excluding the user sending the message.
I am trying to add a reply element to the message.
When i click reply, i use the following code:
<? $reply = $_POST['rfrom']; ?>
<form name='reply' method='post' action='/newmessage.php'>
<input type='hidden' name='rfrom' value='<?php echo $pm->messages[0]['from']; ?>' />
<input type='hidden' name='rsubject' value='Re: <?php echo $pm->messages[0]['title']; ?>' />
<input type='hidden' name='rmessage' value='[quote]<?php echo $pm->messages[0]['message']; ?>[/quote]' />
<input type='submit' name='reply' value='Reply' />
</form>
The values are correct and definately pass the information using POST.
On the initial piece of code I provided, how can I alter this so the username that I am replying to is selected when I am replying, if not, the usernames are just listed.
Thanks

$fromname=(isset($_POST['rfrom'])) ? $_POST['rfrom'] : ''; //ought to validate $_POST
while($row=mysql_fetch_assoc($q)) {
$u=$row['username'];
$selected=($u==$fromname) ? 'selected="selected"' : '';
echo "<option value=\"$u\" $selected>$u</option>";
}

$replyUser = $_POST['rfrom'];
while($row = mysql_fetch_object($q))
{
if($row->username == $replyUser)
{
echo('<option value="'.$row->username.'" selected="selected">'.$row->username.'</option>');
}else{
echo('<option value="'.$row->username.'">'.$row->username.'</option>');
}
}

Related

Select option only posts id, I need the value too

I have an issue in a simple PHP MySQL project. When I try to fetch a record from a select option, it only gives me the id. I want the value from the select to be passed too.
<form class="form-style-9" method="POST" action="function.php?type=updatedistrict">``
<ul>
<li>ID:
<input type="hidden" name="dist_id" class="field-style field-full align-none" value="<?php echo $ids;?>" readonly />
</li>
<li>District Name:
<input type="text" name="name" class="field-style field-full align-none" value="<?php echo $names;?>" />
</li>
<li>Postal Code:
<input type="text" name="code" class="field-style field-full align-none" value="<?php echo $codes;?>"/>
</li>
<li>Province
<select name="province_id" class="form-control">
<option value="">Select Province</option>
<?php
$sql = mysqli_query($conn, "SELECT id, province From province");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['id'] ."'>" .$row['province'] ."</option>" ;
}
?>
</select>
<li>
<input type="submit" value="Update" name="submit" />
</li>
</ul>
</form>
This is the update function code:
case 'updateprovince':
if (isset($_POST['submit'])) {
$id = $_POST['dist_id'];
$prov_id= $_POST['province_id'];
$query=$conn->query("UPDATE province SET province='$prov_id' where id='$id'");
if ($query) {
echo '<script> window.location.replace("province.php");
</script>';
}
else
{
echo "issue";
}
}
break;
You can post your province name instead of the id this way:
echo "<option value='". $row['province'] ."'>" .$row['province'] ."</option>" ;
Your $_POST['province_id'] will then contain the province value instead of the current id.
A way to pass along both the id and the value (in case you need to retain the id for some additional processing) through your option would be this:
echo "<option value='". $row['id'] . '|'. $row['province'] ."'>" .$row['province'] ."</option>" ;
Then after the submit, you'd simply split the two by the '|' delimiter.
if (isset($_POST['submit'])) {
$id = $_POST['dist_id'];
$province = explode('|', $_POST['province_id']);
$province_id = $province[0]
$province_name = $province[1]

add values with one another with input field submit in php calculator

I want a calculator with my PHP code that adds values with one after one with submit button.like when I input a number then submit it and show it on the page then and input other, it should add previous number.and then enter another then submit.like these, numbers are adding with one another after submitting.
<?php
session_start();
?>
<?php
error_reporting(0);
?>
<html>
<title>adding input single values</title>
<body>
<form method="post">
<input type="text" name='number' method="post"/>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number']))
{
}
else
{
$sum += $_POST['number'];
echo ++$sum;
}
?>
</body>
</html>
here is the output
You could use a hidden field instead of storing in the session.
<input type="text" name='number' method="post"/>
<?php
if(!isset($_POST['number'])) {
echo "<input type='hidden' name='prev_number' value=0 />";
} else {
$sum = $_POST['number'] + $_POST['prev_number'];
echo "<input type='hidden' name='prev_number' value=" . $sum . " />";
echo $sum;
}
?>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number'])) {
// ...
}
else {
$_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : '';
$_SESSION['number'] += $_POST['number'];
echo $_SESSION['number'];
}

how to make drop-down form to remember input in php

im creating a form which will display data from database, everything is set so far, but i want the form to remember the data in case if user has to write it down again in case of error. I found some similar code concerning only html, but when including php to display a form i find some difficulties for code to remember the last input (current problem is only concerning drop down selection list):
$Type = $_POST['petType']; //this should remember last input
<?php
/*upload form and drop-down selection list*/
echo "
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>\n
<option value='-1'>Type:</option>";
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
echo "<option value='$petType' ";
if($Type == '$petType')
{
echo "? selected='selected'";
// i need to make selected true only for last selected option,
//and redisplay it in the same form again
}
echo ">$petType </option>";
}
echo "</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
?>
try this:
<?php
$Type = $_POST['petType']; //this should remember last input
?>
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' <?php echo $Type == $petType ? "selected=selected":"";?> ><?php echo $petType;?> </option>
}
</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
sidenote: make html and php different , it makes things easier.
You are comparing $Type to a string not a variable.
Try
if($Type == $petType)

A simple operation but it giving an error I want to update each id

It is simple but I am not getting values of radio buttons for updation when I press submit.
It is giving an error in foreach statement like :Warning: Invalid argument supplied for foreach() Please say how to get the values of radio button after pressing submit button,
here I am updating the status of state .
<?php
include("db.php");
?>
<html>
<body>
<?php
$state=$_POST['state'];
if(isset($_POST['submit']))
{
$result1 = mysql_query("select state,id,status from states ");
while($rr1=mysql_fetch_array($result1))
{
//getting values of radio buttons
$myval=$rr1['id'];
echo $myval;
$val=$_POST['yes.$rr1["id"]'];
echo $val;
$val1=$val.$myval;
$vall=yes.$rr1['id'];
foreach($vall as $values)
{
echo $values;
$update=mysql_query("UPDATE states SET status='". $values."'
WHERE id='$myval' ");
}
}
}
echo "ok";
?>
<form action="" name="form" id="form" method="post" >
<?php
//session_start();
include("db.php");
$result = mysql_query("select state,id,status from states ");
while($info1=mysql_fetch_assoc( $result))
{
echo $info1['city'];
echo "<br>";
/*echo "<br>";
echo "company Name:".$info1['company_name'];
echo "<br>";
echo "salary:".$info1['maxsalary'];
echo "<br>";
//echo $info1['company_name'];*/
?>
<label><?php echo $info1['state']; ?></label>
<input type="radio" <?php if($info1['status']=="yes"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="yes">Yes
<input type="radio" <?php if($info1['status']=="no"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="no">no
<br/>
<?php } ?>
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I think you need to write
$vall=yes.$rr1['id'];
to
$vall="yes".$rr1['id'];
Thanks

Get variable from another conditional block

I want to get a variable from a conditional if of form assigned to take the value of a textbox:
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name; //i got "Notice: Undefined variable: name" here
}
?>
</form>
I want show value of $name after input:name pressed.
This should solve the issue
$name = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show'])) {
echo $name;
}
The problem in your code is that the scope of $name is limited to the first if
Hello and welcome to stackoverflow,
If you want to make your form in 2 steps, you need to store the "name" value in the intermediate form.
<form action="" method="POST">
<input type="text" name="name">
<input type="submit" value="Click Here!" name="submit">
<?php
if (isset($_POST['submit']))
{
$name = htmlentities($_POST['name']);
echo "<input type=\"hidden\" value=\"{$name}\" name=\"name\">";
echo "<br /><input type=\"submit\" value=\"Show it!\" name=\"show\">";
}
if (isset($_POST['show']))
{
$name = htmlentities($_POST['name']);
echo $name;
}
?>
</form>
Several things to point out :
in a field of type "hidden" you store your $name
in such a way you can recover it in the second step
you should also have a look to the htmlentities() function
Hope this helps!
Try it here

Categories