Getting results from database into textarea - html

I have an edit page that fills the content with the original content from the database, I am using inline php this populates the field with the title, and all the other fields work as well. When i try and fill the textarea using the same method it doesn't work.
All the other fields are varchar except the textarea which is text.
The php is in the value of the form.
require_once('includes/db.inc.php');
$stmt = $mysqli->prepare("SELECT
postID,title,content,author,image
FROM posts where postID = ?");
$stmt->bind_param("i",$_GET['postID']);
$stmt->execute();
$stmt->bind_result($postID,$title,$content,$author,$image);
$stmt->fetch();
$stmt->close();
?>
<section id="createPost">
<form method="post" action="editPost.php">
<fieldset>
<legend>Edit Post: <?php echo $title ?></legend>
<input name="postID" type="hidden" value="<?php echo $postID; ?>">
<label for="titleOfPost">Title of Post:</label><br />
<input type="text" name="titleOfPost" size="82" placeholder="Enter title of post" required value="<?php echo $title ?>"><br />
<label for="bodyOfPost">Content of Post:</label><br />
<textarea cols="60" name="postContent" rows="10" placeholder="HTML tags allowed" value="<?php echo $content ?>"></textarea><br />
<label for="authorOfPost">Author:</label><br />
<input type="text" name="authorOfPost" size="82" placeholder="Author name" required value="<?php echo $author ?>"><br />
<label for="imageOfPost">Image:</label><br />
<input type="text" name="imageOfPost" size="82" placeholder="image" value="<?php echo $image ?>"><br />
<input type="submit" name="newPostBtn" value="EditPost" id="newPostBtn"/>
</fieldset>
</form>
</section><!--end createPost-->

Textarea element doesn't a have property value. Use:
<textarea cols="60" name="postContent" rows="10" placeholder="HTML tags allowed"><?php echo $content ?></textarea>

Textareas aren't populated like other input types. The content goes between the tags (like an anchor tag) not within the opening tag (like an image tag).

Related

How to show double quote in input tag?

How to show double quote in input tag ?
my $test is i test "hello"
and input like this
<input name="test" type="text" value="<?PHP echo $test; ?>">
When test code in input show only i test
How can i do for show i test "hello" in input tag ?
Try to use htmlspecialchars or use htmlentities
<input name="test" type="text" value="<?php echo htmlspecialchars($test); ?>">
<input name="test" type="text" value="<?php echo htmlentities($test); ?>">
Or by using PHP echo you can do it
<?php echo '<input name="test" type="text" value="'.$test.'">'; ?>
use html_entity_decode since you're using php like so:
<input name="test" type="text" value="<?PHP echo html_entity_decode($test); ?>">

html 5 datalist value

how can I get the "id" instead of "name" when i submit the form.
<input type="text" list='name' name='test1'/>
<datalist id="test1">
<?php
if(!empty($data)){
foreach($data as $flag){
echo "<option label ='".$flag['id']."' >".$flag['name']."</option>";
}
}else{
echo "<option> empty </option>";
}
?>
</datalist>
I have tried using value = $flag['id'], but I still get "name" when i submit it.
The id is not sent to the server with a regular submitting of a form. Only the name- and value attributes are.
If you really wanted to get the ids too, you could have a hidden <input> element with an array for value, containing all the relevant ids you want.
Here's an example:
HTML
<form method="post">
<input type="text" name="firstname" id="inp1" />
<input type="text" name="surname" id="inp2" />
<input type="hidden" name="ids[]" value="inp1" />
<input type="hidden" name="ids[]" value="inp2" />
<input type="submit" />
</form>
PHP
foreach($_POST['ids'] as $id)
{
echo '<option label="' . $id . '">' . array_search($id, $_POST) . '</option>';
}

Post HTML <form> on load

What is the best way to post a HTML Form on load? This is what I'm currently trying:
<?php
if ($Autopost == "1");
{
<body onLoad="mail.submit()">
<form method="POST" name="mail" class="adjacent" action="./Script/addmaillist.php">
<input type="hidden" name="email" value="<?php echo $email; ?>">
<input type="hidden" name="genre" value="<?php echo $genre; ?>">
</form>
}
?>
I would just like to know if this is a good way, and if there is a better way?
I agree with Kolink that it seems unnecessary to send information to the client and then back to the server, but assuming you need to do that for some reason, you could use Javascript and jQuery to post a form through the $(document).ready() event, which triggers and runs its contents upon page load. So you would have your form with id="mail" and in your script you could have:
$(document).ready(function() {
$("#mail").submit();
}
you don't need any server-side code to submit on-load. also, your code doesn't look quiet right. is that in php?
There are some small syntax errors in your code:
<?php
if ($Autopost == "1")
{ ?>
<body onLoad="mail.submit()">
<form method="POST" name="mail" class="adjacent" action="./Script/addmaillist.php">
<input type="hidden" name="email" value="<?php echo $email; ?>" />
<input type="hidden" name="genre" value="<?php echo $genre; ?>" />
</form>
<?php
}
?>

rest in the same page PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
I have a database and when I sumit info, i get this error:
"Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php:2) in /Applications/XAMPP/xamppfiles/htdocs/mostra/html/noticias/publicacomentario.php on line 14"
I would like rest in the same page When I insert values to database, this my code:
VER.PHP
<form action="noticias/publicacomentario.php" method="post" id="commentform" onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<fieldset>
<input type="hidden" name="noticia_id" value="<?php echo $id; ?>"><br>
<p><label>NOMBRE *</label>
<input type="text" name="usuario"></p>
<p><label for="email">EMAIL (No se publicará) *</label>
<input type="text" name="email"></p>
<p><label for="comment">COMENTARIO</label>
<textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>
PUBLICARCOMENTARIO.PHP
<?php
require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario'];
$email=$_POST['email'];
$comentario=$_POST['comentario'];
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());
$query = "UPDATE noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
header("Location: ver.php?id=$id")
?>
Any idea? thx!
It says line 2, but there's nothing actually on line 2 of PUBLICARCOMENTARIO.PHP.
Ensure that you haven't got any spaces or new lines above "PUBLICARCOMENTARIO.PHP" before the PHP opening tag.
If PUBLICARCOMENTARIO.PHP only contains PHP (No HTML), then you shouldn't require the closing ?> either.
Basically the error is saying that the header("xxx") can't be output as you've output something else already. Usually it's some HTML or some space before the opening PHP tag.
you should remove blank lines before "<?php" in PUBLICARCOMENTARIO.PHP
Check if PUBLICARCOMENTARIO.PHP is saved as UTF8+BOM and if yes, remove the BOM.
You can do this with Notepad++ or any other good editor.
I put the PUBLICARCOMENTARIO.PHP into VER.PHP
<form action="" method="post" id="commentform" onsubmit="MM_validateForm('usuario','','R','email','','RisEmail','comentario','','R');return document.MM_returnValue">
<fieldset>
<input type="hidden" name="noticia_id" value="<?php echo $id; ?>"><br>
<p><label>NOMBRE *</label>
<input type="text" name="usuario"></p>
<p><label for="email">EMAIL (No se publicará) *</label>
<input type="text" name="email"></p>
<p><label for="comment">COMENTARIO</label>
<textarea name="comentario" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p><input type="submit" name="submit" id="submit" tabindex="5" value="Enviar " /></p>
</fieldset>
</form>
</div>
<?php
if ($_POST) {
require ('connect.php');
$id=$_POST['noticia_id'];
$nick=$_POST['usuario'];
$email=$_POST['email'];
$comentario=$_POST['comentario'];
$query = "INSERT INTO comentarios (usuario,email,comentario,noticia_id, fecha) VALUES('$nick','$email','$comentario','$id', NOW())";
mysql_query($query) or die(mysql_error());
$query = "UPDATE noticias SET num_comentarios= num_comentarios+1 where id_noticia='".$id."'";
mysql_query($query) or die(mysql_error());
}
header("Location: $_SERVER[PHP_SELF]?id=$id");
?>
Inside the HEADER("location: ....) i put the $_SERVER[PHP_SELF].
With this works.. but the values ​​entered are not displayed until you refresh the page.. why??

mySQL query returns only one result

I am trying to get each row in a table to appear as part of a survey. The following code is returning only the first row in the table (so users can see only one question). I've been over and over this and can't see what I'm doing wrong. Would much appreciate any input.
Thank you!
function getQuestions ($dbc) <!--$dbc=database connection--> {
$query = "SELECT * FROM survey_questions" <!--survey_questions=table--> ;
$result = #mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC) ) {
$body = $row ['question_body'] <!--question_body=row in table--> ;
echo '
<div class="entry"> <!--user entry form-->
<h3 class="qTitle">'.$body.'</h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
';
}
}
First of all you don't need to echo so much using php...
You have to use // or /* */ to comment in PHP and not <!----> cuz that's for HTML
Secondly coming to your code..
Why you are using?
function getQuestions($dbc) //I dont know what this is doing here, why you are wrapping your code in a function???
you can simply write like this (use echo to print out your question):
<?php
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
?>
<div class="entry"> <!--user entry form-->
<h3 class="qTitle"><?php echo $row['whatever']; ?></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
<?php
}
?>
The results are now two entry forms (an improvement) but still not displaying the text of the two rows from the database. Instead of the row text, I get the > character where the text should be. Here is the updated code, adapting the suggestion of #Mr. Alien:
function getQuestions($dbc) {
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
echo '
<div class="entry">
<h3 class="qTitle">'. $row['survey_questions'].'></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" >
<input type="submit" value="Submit" name="submit" >
<input type="hidden" name="questionid" value="questionid" >
<input type="hidden" name="submitted" value="1" >
</form>
</div>
';
}
}