php forms and url's - php

I'm having some difficulty with php forms.
I have created a page called 'post_details.php' (this simply displays a photo of a product & description). Each product has a unique id
Within posts_details.php, I have used to include command to include a form. This form allows users to send me feedback regarding the product.
For some reason the form is not workin. Everytime the submit button is clicked, the alert box warns me that I need to complete the form (even if the form is complete)
The last part of line one doesn't seem to work. It's not picking up the post_id
Can anyone please help ??
post a comment
<form method="post" action="post_details.php?post= <?php echo $post_id; ?>">
<table width "600">
<tr>
<td>Your email:</td>
<td><input type="text" name="comment_email"/></td>
</tr>
<tr>
<td>Your Comment:</td>
<td><textarea name="comment" cols="35" rows="16"/></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="postcom"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['comment'] )) {
$comment_email = $POST['comment_email'];
$comment = $POST['comment'];
if( $comment_email=='' OR $comment=='') {
echo "<script>alert('Please complete form')</script>";
echo "<script>window.open('post_details.php?post=post_id')</script>";
exit();
}
else {
echo "complete";
}
}
?>

You have error here
if(isset($_POST['comment'] )) {
$comment_email = $POST['comment_email'];
^
$comment = $POST['comment'];
^
....
Instead of $POST it must be $_POST['comment_email'] and $_POST['comment']

Related

Pressing 'update' button in my Php edit page while a field is empty removes the id from the URL

So I have a PHP CRUD system which adds, edits and deletes entries from/to a database. The CMS in question is a drugs table. I can add drugs to this table using the add button and filling in a form, I can also delete the drugs by simply deleting them. But the one of issue is the edit/update part of the system.
The edit feature itself works fine, I'm able to edit an entry and it will post to the database and show up in the table because the user gets redirected back to the table page which shows all the drugs, however, when I remove the text from a field, leaving it empty, and press update, I get an error message which says a text field is empty, however, I also notice the id at the top of the page is no longer there which gives me an Undefined index:error.
I've narrowed the problem down to it being simply because the input type "submit" removes it for some reason.
URL before pressing update with a text field empty:
php_files/DrugEdit.php?Drug_ID=23
URL after pressing update with a text field empty:
eMAR/php_files/DrugEdit.php
How the program works (with code):
Php variable created by getting the Drug_ID from the URL (in this case 23)
$Drug_ID = $_GET['Drug_ID'];
Then gets the field data from the database and assigns them to variables
$result = mysqli_query($conn, "SELECT * FROM drugs WHERE Drug_ID=$Drug_ID");
while($res = mysqli_fetch_array($result))
{
$Drug_Name = $res['Drug_Name'];
$Allergies = $res['Allergies'];
$Side_effects = $res['Side_effects'];
$Type_of_Medication = $res['Type_of_Medication'];
$Dosage = $res['Dosage'];
}
?>
The data is then shown in a form where I can edit the data
<form name="form1" method="post" action="DrugEdit.php">
<table border="0">
<tr>
<td>Drug_Name <?php echo $drug_name_error ?></td>
<td><input type="text" Name="Drug_Name" value="<?php echo $Drug_Name;?>"></td>
</tr>
<tr>
<td>Allergies</td>
<td><input type="text" Name="Allergies" value="<?php echo $Allergies;?>"></td>
</tr>
<tr>
<td>Side_effects</td>
<td><input type="text" Name="Side_effects" value="<?php echo $Side_effects;?>"></td>
</tr>
<tr>
<td>Type_of_Medication</td>
<td><input type="text" Name="Type_of_Medication" value="<?php echo $Type_of_Medication;?>"></td>
</tr>
<tr>
<td>Dosage</td>
<td><input type="text" Name="Dosage" value="<?php echo $Dosage;?>"></td>
</tr>
<tr>
<td><input type="hidden" Name="Drug_ID" value=<?php echo $_GET['Drug_ID'];?>></td>
<td><input type="submit" Name="update" value="Update"> </td>
</tr>
</table>
</form>
If the update button gets pressed, it runs the block of code below:
$drug_name_error = " ";
if(isset($_POST['update']))
{
$Drug_ID = $_POST['Drug_ID'];
$Drug_Name=$_POST['Drug_Name'];
$Allergies=$_POST['Allergies'];
$Side_effects=$_POST['Side_effects'];
$Type_of_Medication=$_POST['Type_of_Medication'];
$Dosage=$_POST['Dosage'];
// checking empty fields
if(empty($Drug_Name) || empty($Allergies) || empty($Side_effects) || empty($Type_of_Medication) || empty($Dosage)) {
if(empty($Drug_Name)) {
$drug_name_error = "<font color='red'>Drug_Name field is empty.</font><br/>";
}
if(empty($Allergies)) {
echo "<font color='red'>Allergies field is empty.</font><br/>";
}
if(empty($Side_effects)) {
echo "<font color='red'>Side_effects field is empty.</font><br/>";
}
if(empty($Type_of_Medication)) {
echo "<font color='red'>Type_of_Medication field is empty.</font><br/>";
}
if(empty($Dosage)) {
echo "<font color='red'>Dosage field is empty.</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($conn, "UPDATE drugs SET Drug_Name='$Drug_Name' ,Allergies='$Allergies' ,Side_effects='$Side_effects' ,Type_of_Medication='$Type_of_Medication',Dosage='$Dosage' WHERE Drug_ID=$Drug_ID");
//redirecting to the display page. In our case, it is index.php
header("Location: ../drug_manager.php");
}
}
Thank you for taking the time to read my problem. Hopefully I've provided enough information for you.
input type "submit" removes it for some reason
Because you're submitting the form, and the URL for the form submit action is defined in the <form> element:
<form name="form1" method="post" action="DrugEdit.php">
Note that there's no Drug_ID value on that URL. However, in the form processing code you're not looking for it in the URL, you're looking for it in the form post:
$Drug_ID = $_POST['Drug_ID'];
So I'm not really sure where you're getting that error. But if somewhere in your form logic you're also looking for $_GET['Drug_ID'], or simply want it to be on the URL for some downstream need, then you can just add it to that URL:
<form name="form1" method="post" action="DrugEdit.php?Drug_ID=<?php echo $_GET['Drug_ID'];?>">

php check text input length and change another input based on length

I have an php form utilizing several inputs that is driving a kiosk page. If a text input is blank I want this update a separate input with the word "hidden" If there is text I would like the word "visible" to show. Currently my code works if you click submit twice but will not work on the first submit. Here is my current code:
The if function that is current working on second submit:
if (strlen($something)>0) {
$_POST['someone'] = "visible";
} else {
$_POST['someone'] = "hidden";
}
input form:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>something : </td>
<td><input type="text" id="something" name="something" value="<?php echo htmlspecialchars($something); ?>"/></td>
<td></td>
</tr>
<tr>
<td></td>
<td><?php echo $_SERVER['PHP_SELF']; ?></td>
<td></td>
</tr>
<tr>
<td>someone:</td>
<td><input type="text" id="someone" name="someone" value="<?php echo htmlspecialchars($someone); ?>"/></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name='submit' value="Submit"/></td>
<td></td>
</tr>
</table>
</form>
Here is the update code:
$usql = "UPDATE test SET something= '".$_POST['something']."', someone= '". $someone ."' WHERE ID='a';";
Currently the "someone" input has a display of none so it cannot be seen by the user. This is not necessary but if someone could tell me how to bypass adding an input altogether and tweak the update statement itself to update something that would be great as well! Thanks!
Any help would be appreciated!
Using session variables can save the page state, even after reload.
First page save:
session_start();
$_SESSION['someone'] = $_POST['someone'];
Then:
if(isset($_SESSION['someone']))
{
if (strlen($_SESSION['someone'])>0) {
$_POST['someone'] = "visible";
} else {
$_POST['someone'] = "hidden";
}
}

Store PHP/SQL foreach form items in variables

Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>

why if (isset($_POST['accept'])) not working?

i have small problem and i don't see any mistakes in my code and server also don't returns any errors but clouse if (isset($_POST['accept'])) doesn't recognize that button was clicked.And is it some way to get value of button id?Please Help.
include '../../config.php';
db_connect();
$zapytanie = "SELECT * FROM messages ORDER BY message_id DESC";
$r = mysql_query ($zapytanie) or die(mysql_error());
$war="L".$_SESSION['lekarz_id'];
echo $war;
if (isset($_POST['accept']))
{
echo "something";
}
else{
while ($row = mysql_fetch_array ($r))
{
if($war == $row['message_recipient'] AND !$row['message_title']=='LP' ){
echo $war;
print "<table border=2 width=98% align=center>
<tr><td><font size=1><p align=left>Od: {$row['message_recipient']}</td> <td><p align=right>Wysłana: {$row['date_of_posting']}</align></font></td></tr>
<tr><td><p align=left><font size=1>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b></td></tr>
<tr><td colspan=2><br /> {$row['message_text']}</align></font></td></tr>
</table><hr />";
}
else if($war == $row['message_recipient'] AND $row['message_title']=='LP'){
echo "
<form>
<table border=2 width=80%>
<tr>
<td>Od:Wysłana: {$row['date_of_posting']}</td>
<td><p align=center>Wysłana: {$row['date_of_posting']}</align></font></td>
</tr>
<tr>
<td><p align=center><font size=5>Do:{$row['message_sender']}</td><td><b><p align=right>Temat:{$row['message_title']}</align></b>
</td>
</tr>
<tr>
<td ><br /> <font size=5>{$row['message_text']}</align></font>
</td>
</tr>
<tr>";
$id = $row['message_sender'];
$id=substr($id, -1);
print'
<td>
<form action="messages.php" method="post">
<input type="submit" name="accept" value="accept" id=.{$id}./>
</form>
</td>
<td> </td>
</tr>
</table>
</form><hr />
';
}
}
db_close($db);
}
?>
You didn't explicitly set the form action to POST. As a result your form values willbe sent via GET.
Change:
<form>
to
<form method="POST">
Or look for for your value in the $_GET superglobal.
if (isset($_GET['accept']))
You also have a form embedded within a second form. Removing the outer form would also solve your problem.
Check for $_REQUEST['accept']. This combines $_GET, $_POST and $_COOKIE. It will contain the form value regardless of submission method.
http://www.php.net/manual/en/reserved.variables.request.php
I also have to mention, that it's not a good idea to use submit button value to send parameter. What if you need to write "Accept" instead of "accept" on your button?
if ($_POST['accept']=='accept') will fail for such button. Better practice is to use hidden field with name="accept" (<input type="hidden" name="accept" value="any_value" />) and let submit button to do what it has to do - to submit form.
Also it makes your form reusable
phpfidle

If a file hasn't been chosen then don't upload

I'm having trouble with this form. I want the user to be able to edit an item in the datebase. They chose which item they want to edit on one page and get sent with a GET to the editing page. The GET has the id of the item they need to edit.
The editing page loads with the details of the item inserted into the user form (apart from the name of the file) this field is left blank. I am trying to do some logic that checks if the user has chosen a file.
If they haven't then this field should be ignored as I will presume the user is happy with the file that is already uploaded.
If they chose a file then this means they want this to be the new picture. Only then do I want to run the logic to upload the picture and insert its name into the database.
I'm getting my POST details by saying:
$clean_pic = $_POST['pic'];
I am then saying if it's blank do nothing otherwise run the upload:
if($clean_pic = ''){}
else{
It's not working. Any ideas how I should find out if its blank? Cut down code:
if (isset($_POST['add']))
{
// validate 'pic': must consist of alphanumeric characters only.
$_POST['pic'] = isset($_POST['pic']) ? $_POST['pic'] : '';
//if(preg_match('/\.(jpg|gif|jpeg)$/i',$_POST['pic']))
//{
$clean_pic = $_POST['pic'];
//}
//else
//{$error++; $errmsg .= 'Invalid pic. ';}
}
if (isset($_POST['add']) && ($error==0))
{
if (!isset($_POST['pic'])) { echo"test1";}
else {echo"test222";}
/*tied this too but it didnt work (it will always display result 1):
if (!isset($_POST['pic'])) { echo"test1";}
if (isset($_POST['pic'])) {echo"test222";}*/
}
else //output error messages
{
/////////render form
?>
<form enctype="multipart/form-data" action="" method="post" id="save"><fieldset>
<table id="site-form">
<tr>
<td class="one_of_three"><label>Item Name: </label></td>
<td class="two_of_three"><input type="text" name="fileName" id="fileName" value="<?php echo"$db_name";?>"/></td>
<td><label class="errors" id="fileNameError"> </label></td>
</tr>
<tr>
<td class="one_of_three"><label>Picture: </label></td>
<td class="two_of_three"><input type="file" name="userfile[]" id="pic"/></td>
<td><label class="errors" id="picError"> </label></td>
</tr>
<tr>
<td class="one_of_three"> </td>
<td class="two_of_three"><input name="add" id="save_button" type="submit" value="Update"/> Cancel.</td>
<td> </td>
</tr>
</table>
</fieldset></form>
<?php }?>
What about if (!isset($_POST['pic']))?
You cant access the file using 'id' attribute ('pic'). You have to use the value of 'name' attribute ('userfile'). You can check whether the file is uploaded or not using $_FILES array in php.
if(isset($_FILES) && isset($_FILES['userfile']) && (trim($_FILES['userfile']['name']) != '') ) {
//Your upload code here
}
else
{
//No file uploaded
}
You need to check to things :
one is $_POST['pic'] is an array
second one is in the form you have to use enctype="multipart/form-data" attribute.

Categories