I have a simple contact form on a website that has 2 text fields, 1 textarea, and 1 hidden field.
For some reason, all the fields POST to a PHP script except the textarea. I have done this a thousand times before and never had this issue.
Here is my HTML:
<form action="scripts/contactform.php" method="post">
<table width="0" border="0" cellspacing="3" cellpadding="5" class="gpass">
<tr>
<td>Name:</td>
<td><input name="name" type="text" maxlength="50" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input name="email" type="text"/></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="comment" id="comment" cols="30" rows="5"></textarea>
<input type="hidden" value=" <?php echo $_SERVER['REMOTE_ADDR'];?>" name="address" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" value="Submit" class="noround" id="regbut" /><input name="reset" type="reset" value="Reset" class="noround" id="regbut"/></td>
</tr>
</table>
</form>
And my script looks like this:
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}
$db = mysql_select_db(DB_DATABASE);
if(!$db)
{
die("Unable to select database");
}
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$comment = mysql_real_escape_string($_POST['comment']);
$ipaddress = mysql_real_escape_string($_POST['address']);
I have a few things to process the data underneath this, but that doesn't matter since the $comment variable isn't being defined. I've searched the entire script and there are no conflicting variable names.
I am completely stumped on why this is happening. I've successfully processed textarea's on my site multiple times before, so this really is confusing.
I once experienced an error similar to yours. What helped me was to use different id and name parameters. Try and see for yourself, because you have them identical here.
Though in your case you don't have the textarea set to disabled, the reason I found this post was because I wasn't getting a value from a textarea that was. So here's a note for anyone else with that issue.
To POST the value from a textarea where you want the field to be non-editiable, use readonly instead of disabled - either directly in html or via setAttribute in JavaScript - and then use CSS to highlight it, eg:
textarea[readonly] {background-color:#F0F0F0;})
You only need to add an ID value to the form and then add the form attribute to the textarea with the form ID value
<form id="sample".....>
<textarea name="aba" form="sample".....></textarea>
</form>
Related
I am facing problem in textarea content POST via PHP. I have a form that submits two values one in textarea and other radio button. while submitting the radio button value is posted but textarea value showed up empty.
why is this happening? Any suggestion would be appreciated..
My snippet of HTML Code
<form action="" method="post" id="register_form">
<h2><img class="small_image" src="images/rsz_heart.png">Describe
Yourself<img class="small_image" src="images/heart.png"></h2>
<table id="register_table">
<tr>
<td>Describe Yourself</td>
<td>
<textarea id="description" type="textarea" name="description" rows="5"
cols="40" class="textbox" form="register_form" required>type</textarea>
</td>
</tr>
<tr>
<td>Any disability</td>
<td>
<input type="radio" name="disability" value="none" selected="selected">None
<input type="radio" name="disability" value="physicaldisability">Physical
Disability
</td>
</tr>
<tr>
<td colspan=2>
<input type="submit" name="submit" value="Submit" class="button" >
</td>
</tr>
</table>
</form>
My PHP code is
if(isset($_REQUEST["submit"]))
{
$description = $_POST["description"];
$disability = $_POST["disability"];
$email = $_GET["email"];
$sql = "update Registration_members set Description_self='$description',
Disability='$disability' where Email='$email'";
$res = mysql_query($sql);
if($res)
{
?>
<script>
alert("You have registered successfully!!");
</script>
<?
echo $description." is description";
echo $disability." is disability";
}
}
?>
In output it writes
is description
none is disability
Some edits to your code may solve your problem:
1) Always use <?php as starting PHP tag. You have used only <? at one place in your code. Change that.
2) Change isset($_REQUEST["submit"]) to isset($_POST["submit"])
3) Remove type="textarea" from your <textarea>
4) Be careful while opening and closing PHP tags. In your case, you have closed PHP tag just after if(isset($_REQUEST["submit"])) { which is wrong.
You are closing php tag too early.
Replace
<textarea id="description" type="textarea" name="description" rows="5"
cols="40" class="textbox" form="register_form" required>type</textarea>
by
<textarea id="description" name="description" rows="5"
cols="40" class="textbox" form="register_form" required></textarea>
type="textarea" is wrong, also use $_POST instead of $_REQUEST["submit"]
You need an opening <?php tag or your code will not be interpreted as PHP.
In the original version of your question, on your third line, you had ?>. That closes the PHP block and means the next chunk of code is treated as plain HTML. So, it's never evaluated. Delete that line.
On that note, later in your code, you should use <?php, not <?, to start a new PHP code block.
Also, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead. On top of that, you are wide open to SQL injection.
I have created a blog using PHP that submits a form into a MySQL databse. It works perfectly. What i want to know is how to get it to recognise when i've pressed enter to create a new line or to record a ' without getting a MySQL error. Basically i want to turn a normal submission form into a submission form like is used on this website.
<tr>
<td>Blog Title:</td>
<td><input type="text" id="edt" name="BlogTitle"></td>
</tr>
<tr>
<td>Blog Content:</td>
<td><textarea name="BlogCont" cols="50" rows="10" id="edt"></textarea></td>
</tr>
<tr>
<td><input type="hidden" name="Name" value="<?=$_SESSION['Name']?>"></td>
<td><input type="submit" id="but" value="Post"></td>
</tr>
Any help would be appreciated.
For new lines with enter use nl2br (manual), for apostrophe you have to escape it with mysql_real_escape_string (manual).
Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download
I've not done any coding in a while, but needed a quick way to send an email to a few people at a time with using two variables. Should be simple, but I have no idea why this isn't working.
Thanks in advance.
<?php
if(!empty($POST['update']))
{
echo 'it works!';
}
else
{
?>
<h1>Order Confirmation</h1>
<form method="post" action="order-confirmation.php" name="update">
<table>
<tr>
<td>Account Number</td>
<td>Consignment Number</td>
</tr>
<tr>
<td><input type="text" name="accno" value=""/></td>
<td><input type="text" name="conno" value=""/></td>
</tr>
<tr>
<td><input type="submit" name="submit" action="order-confirmation.php"/></td>
</tr>
</table>
</form>
<?php
}
?>
It should be
$_POST
Instead of
$POST
Also, you want it to be $_POST['submit'] instead of update.
You do not have input field called "update".
You must also replace $POST with $_POST and add an <input type="hidden" name="update" value="1" /> to your form.
type="submit" does not need action attribute because you already have defined action in your <form>.
I usually use something like this (anything with // before mean comment, not executable code)
//Request method detect that POST is used not GET which mean the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST) {
// This condition detect that the input with name "submit" is pressed, you can
// add multiple submit buttons and each with different value, then just do
// equality check to specify the actions
if ($_POST['submit']) {
echo 'it works!';
}
}
So Im relatively new to PHP from ASP. And After converting alot of ASP code into PHP I have come across a problem where my PHP code seems unable to find the hidden variable I have set. It worked fine in ASP and was just wondering the best way to resolve this.
Start of the Form:
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
<table align="center" cellSpacing="2" cellPadding="2" border="0">
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">User name:</font> </td>
<td><input name="UserName" size="25" type="Text" autocomplete="OFF"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="right"><font color="#4d71a1">Password:</font> </td>
<td><input name="Password" size="25" type="Password" autocomplete="OFF"></td>
<td> </td>
</tr>
PHP script:
<?
if ($_POST["BtnPress"]=="Pressed")
{
if ($_POST["Username"]=="*****" && $_POST["password"]=="*********")
{
$_SESSION['AdminID']="1";
header("Location: "."index.php");
}
else
{
print "<font color=#ff0000>Sorry you cannot access this part of the site.</font>";
}
}
?>
then the rest of the form:
<tr>
<td align="center" colspan="4">
<input type="hidden" name="BtnPress" value="Pressed">
<input type="Submit" value="Log In" class="mybutton" onclick="return CheckForm();">
</td>
</tr>
</table>
</td>
</form>
The PHP seems unable to find the variable BtnPress, its a similar problem throughout alot of my translated ASP to PHP script. Sorry if it is a simple solution but can anyone tell me where I am going wrong?
You have name="Password" and $_POST["password"]
Password != password
Watch your case.
<form name="LogIn" action="login.php" method="post">
<td bgColor=#ffffff>
That is invalid HTML. A <td> element cannot be a child element of a <form>. Browsers are likely to error recover in ways that break your HTML (e.g. by moving the form, but not its contents, outside the table). Do use a validator.
And stuff that isn't likely to be the cause of the problem, but is likely to be the cause of other problems.
Don't use layout tables
Do use the label element
Do use CSS for presentation (you can style your label elements instead of using the obsolete font element
Do not use a hidden input to test if a form is submitted.
use:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// processing of $_POST
}
The name of the submit button should be "BtnPressed" not the hidden field.
What I would do would be to set the form action to ?id=submit or something like that, and then check "if $_GET['id'] == "submit" then process data.
<form action="login.php?do=submit">
.... <input .....
</form>
<?php
if($_GET['do'] == "submit"){
//process data
} ?>