Radio button with text field into SQL - html

I'm making a website where users can insert data into a database and keep track of their medicine intake.
I have a simple form that submits to an SQL database:
<form method='post'>
<b>Dato:</b> <input type="date" name='date' id='date' value="<?php echo date('Y-m-d'); ?>" /><br /><br \>
<b>Tid:</b> <input type="time" name='time' id='time' value="<?php echo " $hour:$minute "; ?>" /><br /><br \>
<b>Medicin:</b><br /> <input type="radio" name="medicin" value="Medicin1" checked> Medicin1 <br />
<input type="radio" name="medicin" value="medicin2"> Medicin2 <br />
<input type="radio" name="medicin" value="other"> Other <input type="text" name="otherMedicin"><br /><br />
<b>Kommentar:</b><br /><textarea name='comment' id='comment'></textarea><br /> <hr \>
<input type='submit' value='Send' />
</form>
To submit it to the database I found a previous question about the same thing that I follow. Unfortunately I just can't get it to work!
html radio button with an "other" selection that has a text box
Here is the important part of my code:
$users_date = mysqli_real_escape_string($con, $_POST['date']);
$users_time = mysqli_real_escape_string($con, $_POST['time']);
$users_medicin = mysqli_real_escape_string($con, $_POST['medicin']);
$otherMedicin = mysqli_real_escape_string($db, $_POST['otherMedicin']);
if ($users_medicin == 'otherMedicin') {
$medicinField = $otherMedicin;
} else {
$medicinField = $users_medicin;
}
$users_comment = mysqli_real_escape_string($con, $_POST['comment']);
$query = "
INSERT INTO `myDatabase`.`medi_list` (`id`, `dato`, `tid`, `medicin`, `kommentar`) VALUES (NULL, '$users_date',
'$users_time', '$medicinField', '$users_comment');";
I'm still quite new to SQL so I'd appreciate any help you can throw my way :)

I see some basic problems in your code:
When you are populating the field "$otherMedicin", you are using $db instead of $con like the other statements.
$_POST[medicin] is NEVER going to be "otherMedicin", because that is the name of your text input field, not the radio button name. You should have this as your conditional:
if ($users_medicin == 'other') {
That should help a bit.

Related

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

E-mail box for HTML website

So I have made myself a website. Since it has plans for being used I need someones help :)
I need users to fill in a few boxes and then send it to my email at thisismyemail#provider.com.
Since I am a little new I dont now how to do it.
The form: http://imgur.com/U5Q3jrE
This is my code:
<form action="../index.html" method="post" class="message">
<input type="text" value="Naam" onFocus="this.select();" onMouseOut="javascript:return false;"/>
<input type="text" value="E-mail" onFocus="this.select();" onMouseOut="javascript:return false;"/>
<input type="text" value="Onderwerp" onFocus="this.select();" onMouseOut="javascript:return false;"/>
<textarea></textarea>
<input type="submit" value="Send"/>
</form>
Now what I need is that it doesnt bring me to the index page but show a message with someone like: "Your message has been send" and that it sends it to my email, because at the moment it doesnt send it to anything.
Once more I am pretty new so please forgive my noobness :P
Thanks everyone,
Waylon194
Ok here's a php html code that can be used to email, that you can start with.
User inputs must always be sanitized accordingly. This code is just for demonstration.
<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['email'])) {
//Email information
$admin_email = "someone#example.com";
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$comment = $_REQUEST['comment'];
//send email
mail($admin_email, "$subject", $comment, "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
<form method="post">
Email: <input name="email" type="text" /><br />
Subject: <input name="subject" type="text" /><br />
Message:<br />
<textarea name="comment" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
<?php
}
?>

using implode inside insert query

How can I use implode inside query to insert the values into db. here is my example:
Html form :
<form action="insert.php" method="post">
name:<input type="text" name="name" /><br />
address:<input type="text" name="address" /><br />
phone:<input type="text" name="phone" /><br />
<input type="submit" name="insert" value="insert" />
</form>
I use this code to get _POST values from the application form:
$x = array_values($_POST);
and here to put "," comma between the strings so I can use it in the query :
$x = "'".implode("','",$x)."'";
here is what I did:
mysql_query("INSERT INTO dbtable (name, address, phone)
VALUES
($x)");
When I echo $x:
the results is: 'name','address','phone','insert'
the code print "insert"<- it's the submit button. I think this is the problem
can anyone help me to fix my mistake please
Thanks
the implode shuld be like:
$x =implode("','",$x)."'";

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