I'm building a restaurant menu application in PHP & JavaScript
And I can't seem to get the buttons to append the text area when the ordering is being placed...(This is just test code at present) Here's my code:
$test = mysql_query("SELECT * FROM main_stock");
while($row = mysql_fetch_array($test)){
echo "<div id='".$row["RCode"]."'><a href='javascript:addTo(".$row["Name"].",".$row["RCode"].")'>".$row["Name"]."</a></div>";}
And my javascript function is as follows:
function addTo(name, Rcode) {
document.getElementById('order').value += name;
}
And HTML Form is as follows:
<form id="OrderForm" name="OrderForm" method="post" action="">
<p>
<label>
<textarea name="order" id="order" cols="35" rows="20" readonly="readonly">test</textarea>
</label>
</p><table>
<tr><td>Subtotal:</td><td><input type="text" id="subtotal" readonly="readonly" value="2.00" /></td></tr>
<tr><td>Tax:</td><td><input type="text" id="tax" readonly="readonly" value="2.00"/></td></tr>
<tr><td>Total:</td><td><input type="text" id="total" readonly="readonly" value="4.00"/></td></tr>
<tr><td></td><td><input type="submit" id="submit" value="Send Order To Kitchen"/></td></tr>
</table>
Looks like the echo may be resulting in invalid JavaScript syntax. E.g.:
addTo(Something, Inc.,STI)
Rather than:
addTo("Something, Inc.","STI")
You'll need to output additional quotes for JavaScript to use. And, since you're already using " for the PHP string and ' for HTML attributes, this will require escaping -- or encoding:
echo "... href='javascript:addTo("".$row["Name"]."","".$row["RCode"]."")'>...";
Another option may be to use json_encode since JSON is based from JavaScript syntax:
echo "... href='javascript:addTo(".json_encode($row["Name"]).",".json_encode($row["RCode"]).")'>...";
TEXTAREAs don't have a .value, they have .innerHTML
Try
document.getElementById('order').innerText += name;
There's nothing with ID 'order' in your example.
Instead of document.getElementById('order'), try document.getElementById(Rcode).
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 need to copy the input text to an array like this:
If the input is "12345":
<input type="text" name="data1">
Then in text input (array) also written "12345"
<? foreach ($countries as $data2)
{
<input type="text" name="$data2['location'][]">
}
<script language="javascript">
function copy()
{
document.form1.data2.value=document.form1.data1.value
}
</script>
<form action="" method="post" name="form1">
<input type="text" name="data1" onKeyUp="copy()"/>
<br /><br>
<input type="text" name="data2">
</form>
no problem...
But if like this:
<input type="text" name="data2[]">
<input type="text" name="data2[]">
<input type="text" name="data2[]">
OR in array PHP, like this :
<?php foreach ($countries as $data2): ?>
<input type="text" name="<?php echo $data2['location'][] ?>" />
<?php endforeach ?>
dont work...
It seems you need to open and close your PHP tags in the right place. If the name of your HTML input control is $data2['location'][], then try this:
<?php foreach ($countries as $data2): ?>
<input type="text" name="<?php echo $data2['location'][] ?>" />
<?php endforeach ?>
So, I've closed PHP mode before and after both the loop keywords (the start of the loop and the end of it). This means that everything outside of those tags is in HTML mode, so to get the name of the control, we need to open the tags again. I've chosen to use <?php rather than <? because the latter can be turned off on some servers. However, it is fine to stick with the short form if you don't mind that the code is not portable.
Notice also that I've used the colon with the more explicit end keyword. This is a popular way of constructing loops in the view layer; for ordinary controller code, I suggest you stick to ordinary braces.
I have a form with some jQuery UI sortables that have data-attributes that get serialized into a hidden textbox, so when I send the form, my PHP script can look at the value of $_POST["contents"](which is a string like this 1-2-2-1) and pass it to my MySQL UPDATE query. The problem is, the reported value of the textbox before sending the form (from the javascript), is different than the one in $_POST.
The form:
<form method="post" action="?update">
<input type="text" name="id" style="display:none" value="1" />
<input type="text" name="path" placeholder="Endereço Ex.: /pacotes/pureenergy" value="/some/path" /> <input type="text" name="title" placeholder="Título da página" value="<?php echo $t;?>" /><br />
<!-- Some stuff hidden for briefness -->
<input type="text" id="ci" name="contents" style="display:none" value="1-2-2-1" />
<input type="submit" value="Atualizar página" onclick="updateForm()">
</form>
The Javascript:
function updateForm() {
var txt = $("#dropzone li:first-of-type").data("id");
$("#dropzone li:not(:first-of-type)").each(function() {
txt = txt+"-"+$(this).data("id");
});
$("#ci").text(txt);
alert(txt);
}
The PHP:
$id = $_POST["id"];
$p = $_POST["path"];
$t = $_POST["title"];
$c = $_POST["contents"];
mysql_query("UPDATE aruna.pages SET path='$p', title='$t', contents='$c' WHERE id=$id") or die(mysql_error());
The value reported by the javascript alert() is behaving as intended, but when I send the form, the value of the $_POST["contents"] is the same that was hardcoded in the HTML.
EDIT: PHP doesn't complain about $_POST["contents"] being unset, which it would if there was a typo.
You may try .val() instead of .text()
Suppose I have a form like this:
<form action="page.php" method="post">
Section1: <input name="section1" type="text"></br>
Section2: <input name="section2" type="text"></br>
Text:</br>
<textarea name="post_text" cols="100" rows="20"></textarea>
<input name="submit" type="submit" value="Submit">
</form>
Usually if I wish to save content inserted in a field of the form I would use this statement:
Section1: <input name="section1" type="text" vale="value="<?php echo $_POST['section1']; ?>""></br>
This way if I make a mistake in submission (error control code is not posted) the values inserted will be kept, and there is no need to reinsert them.
However using it in the textarea tag it will not produce the desired result.
Any ideas on how to do?
Thanks in advance!
Don't forget about htmlspecialchars(). This should help: https://developer.mozilla.org/en/HTML/Element/textarea
<textarea name="post_text" cols="100" rows="20"><?php echo htmlspecialchars($_POST['post_text']);?></textarea>
You could use the same approach, but put the echo between the opening and closing <textarea></textarea> tags, as the textarea doesn't have a 'value' (as such) it has textual content:
<textarea name="post_text" cols="100" rows="20"><?php echo $_POST['textareaContent']; ?></textarea>
Use the $_POST variable like this.
<textarea name="post_text" cols="100" rows="20"><?= isset($_POST['post_text'])?$_POST['post_text']:'' ?></textarea>
the inline conditional checks if the $_POST['post_text'] is set to remove the NOTICE warning
You would put it inside the <textarea> element like so:
<textarea name="post_text" cols="100" rows="20"><?php echo $_POST['post_text']; ?></textarea>
However, calling the $_POST element directly is not best practice. You should rather do something like this:
<textarea name="post_text" cols="100" rows="20">
<?php echo $var = isset($_POST['post_text']) ? $_POST['post_text'] : ''; ?>
</textarea>
This stops an E_NOTICE error from being reported upon the first page-load.
How to add values from <input> to $var in php file?
html
<input type="text" id="align" name="align"/>
php file
<?php
$align="center";
?>
You mean you want to post it and put it in a PHP variable?
Try this:
<form action="somephpfile.php" method="POST">
<input type="text" name="align" value="center" />
<input type="submit" value="Send!" />
</form>
somephpfile.php
$align = $_POST['align'];
echo $align;
It depends on the form action.
If your form that is holding your fields has an action="post" attribute then from the php side you have to use $_POST['align']. If you have set the action to action="get" then you have to use the $_GET['align'].
<?php
$align = $_POST['align'];
// OR
$align = $_GET['align'];
?>
Just output it into the regular HTML value attribute:
<input type="text" id="align" name="align" value="<?php echo htmlspecialchars($align); ?>" />
The htmlspecialchars() call is there to escape things like quotes, to avoid problems if your variable contains quotes, and to make your mark-up more standards-compliant.