rest in the same page PHP [duplicate] - html

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??

Related

Turn a datalist that gets users from MySQL into Combo Box

I'm using PHP and MySQL to import a list of users into my dropdown box on a form. The user will select their name and move on with the form... my issue at this point is I want to take the drop down box and make a combo box out of it so that the user can quickly type and select there name however I'm not sure of the best way to input that into my php / html code... I'm coming back to this after a couple days away from it so I don't have the code I've tried in the past with a combo box but I could never get it to quite format the names right... what would be the best way to do this?
$datalist = "<select id='soflow' name='name'>";
foreach ($stmt as $row) {
$datalist .= "\r\n<option value='{$row['FULLNAME']}'>{$row['FULLNAME']}</option>";
}
$datalist .= "\r\n</select>";
echo $datalist;
$dbh = null;
?>
</select><br>
<br></br>
<input type="hidden" name="topicid" value="29">
<input type="text" name="email" placeholder="Phone Ext"><br>
<input type="text" name="subject" placeholder="Title of Request"> <br>
<textarea placeholder="Put your message here..." cols="50" rows="4" name="message"> </textarea><br>
<input type="submit" name="submit" value="send" />
Solved it with J Query UI which included a option inside the ui!

How to submit a contact form via custom button using "href" code?

For my website I'm working on a contact form, which works. But I want the submit button to be like other buttons on my site, meaning I want it to be styled like the code below.
However, I can't get it to submit via a "href" code.
I've tried applying answers on similar questions but haven't had any luck.
Any help would be greatly appreciated!
THE STYLE FOR THE SUBMIT BUTTON I WANT TO USE:
<div class="section-buttons">
<p class="button layer" data-depth="0.10" ><a href = "I WANT THIS TO SUBMIT THE FORM"
class="y1 knop roll swing"><span class="g1">SUBMIT</span><span class="g2">SUBMIT</span>
</a></p>
</div>
THE FORM CODE:
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required placeholder="Name">
<label></label>
<input name="email" type="email" required placeholder="E-mail">
<label></label>
<textarea name="message" cols="20" rows="5" required placeholder="Message">
</textarea>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
EDIT:
Thanks a lot for the quick replies. Really appreciated.
Celt, your solution definitely brings me close to the solution.
It does indeed bring me to the form.php but it doesn't seem to submit the actual data to an email address.
Could it be that my PHP file (forms.php) does something wrong with the new code? I'm guessing the last part "if ($_POST['submit'])" doesn't quite work with this new approach? Any help?
Sorry for the noobness.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website.com';
$to = 'email#email.com';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thank you for your email!</p>';
} else {
echo '<p>Oops! An error occurred. Try sending your message
again.</p>';
}
}
?>
This should work for you:

Joomla 1.5 Database Query example needed

I have made a query, using mysql_* way, that select's some values from a table, where row's id is 1. Then I put all those values in variables so I can echo them in to my form. How can I do the same thing using Joomla's database query way ?
Here is an example of my code that working, using mysql_*:
<?php // DATABASE QUERY
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$result=mysql_query($query);
// DATABASE VARIABLES
$countdown_module = mysql_result($result,$i,"countdown_module"); ?>
$hometeam_header = mysql_result($result,$i,"hometeam_header"); ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>
OK I found it!!! Here is an example...
<?php // DATABASE QUERY
$db = JFactory::getDBO();
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$db->setQuery($query);
$rows = $db->loadObjectList();
$itemrow = $rows[0];
// DATABASE VARIABLES
$countdown_module = $itemrow->countdown_module;
$hometeam_header = $itemrow->hometeam_header; ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>

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>
';
}
}

Getting results from database into textarea

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).