I have a textarea in a form that im trying to POST into a mysql database. However, when i insert the values, i get about 5 lines in the database. 4 of them have no information or minimal information and one of the lines is how i want it. How can i get rid of the other four lines? thanks.
EDIT: actually none of the lines have all the right info in the right spots and i have double checked the inset statement to make sure everything lines up.
the code:
<form method='POST' action="index.php">
<textarea id ='answerbox' autocomplete='off' cols="80" rows="5" name='answer'></textarea>
<input type='submit' value='submit'>
<?php
include 'connect.php';
$date=date("Y-m-d");
$time=time();
$answer=['answer'];
$user=$_SESSION['username'];
$id=$_GET['id'];
$put=mysql_query("INSERT INTO solutions VALUES ('','0','$date','$time','$user', '$answer')");
?>
Try:
<?php
if($_POST['answer'] != '')
{
include 'connect.php';
$date=date("Y-m-d");
$time=time();
$answer= $_POST['answer'];
$user=$_SESSION['username'];
$id=$_GET['id'];
$put=mysql_query("INSERT INTO solutions VALUES ('','0','$date','$time','$user', '$answer')");
}
?>
Your textarea is named as "answer" while its ID is "answerbox". You should be able to retrieve its value through $_POST['answer'];
Related
I'm trying to load an array (with simple text) and trying to load it up on the template whenever it is called. How do I get the ID from that specific item to get the array that I stored in it?
HTML Template:
<template name="commentMarker">
<div id="viewMarker">
<h3 id="markerTitle">{{markerName}}</h3>
<h6 id="markerCategory">{{markerCategory}}</h6>
<br>
<fieldset>
<legend>Description</legend>
<p>{{markerDescription}}</p>
</fieldset>
<form id="commentForm">
<fieldset>
<legend>Comments</legend>
<input type="text" id="markerId" name="idForComment" value={{markerId}}>
<textarea rows="3" cols="19" name="comment" id="commentArea" placeholder="Insert your comment here..."></textarea>
{{#each comments}}
<p id="oneComment">{{this}}</p>
{{/each}}
</fieldset>
<input type="submit" value="Comment" class="commentButton">
<input type="submit" value="Close" class="exitButton">
</form>
</div>
</template>
JS:
Template.commentMarker.helpers({
comments(){
alert(template.find("#markerId").value);
if(commentArray.length===0) return;
else return commentArray;
}});
This is where I insert the comment into the collection's item and it's working fine
Template.commentMarker.events({
'click .commentButton': function(e, template){
e.preventDefault();
var id = template.find("#markerId").value;
var comment = template.find("#commentArea").value;
Points.update(id, { $push: { comments: comment }});
commentArray = Points.findOne(id).comments;
template.find("#commentArea").value = ' ';
}
I tried with commentArray as a global variable which still is. But I'm at loss how I can get the Id from that specific item, I even put it's Id (with hidden display) in the form to actually be able to insert the comment. But it doesn't help me with showing the comments because I cannot seem to get to this field in the Template.helpers ...
Not entirely sure what you are trying to do. It's almost like as if you are displaying the comments right after you updated in to the collection. It looks like you are doing this entirely on local and not a online collection.
However, storing it as a session would work...or reactive var. Might not be the best solution tho. Basically replace commentArray = Points.findOne(id).comments; with:
Session.set('comments', Points.findOne(id).comments)
Then to get it out in helpers:
let commentArray = Session.get('comments')
It's not safe to use it all the time tho for sensitive data. Also try catch the findOne(id).comments because it does produce errors if it happen to not find it.
NOTE: If you are going to use Meteor.Methods, you cannot use Session. You have to return the id and find it in your helpers.
I have a really basic Date system on a site which displays the dates from the Database using a While loop, next to each date that is listed is a check box.
I would like the user to be able to select as many dates as they would like and click submit to remove these dates or edit these dates.
I am having trouble figuring out how I would get the data from multiple check-boxes using only one within the while loop which could be used multiple times?
Here is my current code:
<form action="" method="post">
<b>Current Dates:</b> <button type="submit" id="remove_dates" name="remove_dates" class="btn btn-danger btn-sm">Remove Selected</button>
<hr>
<div style="overflow-y: scroll;height:100px">
<?
$sqldate = "SELECT * FROM `foiu51r_calendar` ORDER BY date DESC";
$resultdate = $conn->query($sqldate);
if ($resultdate->num_rows > 0) {
// output data of each row
while($rowdate = $resultdate->fetch_assoc()) {
$date_id = $rowdate['date_id'];
$dates = $rowdate['date'];
?>
<input type="checkbox" id="date_id" name="date_id" value="<? echo $date_id; ?>"> <b><? echo date("d/m/Y", strtotime($dates)); ?></b><br>
<?
}
}
?>
</div>
</form>
if(isset($_POST['remove_dates'])){
$date_id = $_POST['date_id'];
}
I believe if you add this: [], to the name attribute, like this: <input type="checkbox" id="date_id" name="date_id[]" value="<? echo $date_id; ?>">. Then it will be handled as an array, which you can loop through with a foreach loop. and insert it into a database (or whatever you like).
Declare the name as an array, like as follows:
<input type="checkbox" id="date_id" name="date_id[]" value="<? echo $date_id; ?>">.
On submitting it will be handled as an array,
Then, you can loop the values of an array and for each element of an array use the id to remove the dates.
I'm asked to write an online test. I have 2 tables, one includes questions (2 fields, one for id and one for question text). And another table for saving answers (with 3 fields, one for id, one for question id and one for the answer).
Here's how I've written it so far:
<form name="test" method="post" action="index.html">
<div class="slideshowContainer">
$question_query=dbquery("SELECT * FROM questions");
while($question_array=dbarray($question_query)){
echo'
<div class="question">
'.$question_array['question_text'].'
<br />
<textarea name="'.$question_array['id'].'"></textarea>
</div>
';
}
echo'
<input id="finish" type="submit" name="finish_test" value="send" />
</div>
</form>';
I'm also using cycle plugin so users can see the questions as slides and write the answers in each textarea provided for each specific question.
What I can't do is the way I should write the if(isset($_POST['finish_test'])) part. Because as I know in this way, the received data is in the form of an array. So I thought I might need another for or while element when I'm gonna save the answers and send them to my database.
What can you suggest?
Sure you are gonna need to loop through the results of the $_POST but its nice to make different id for each of the text areas in your code. That way you can identify which are which answers. So to do that I would do something like this.
while($question_array=dbarray($question_query)){
i = 0;
echo'
<div class="question".i>
'.$question_array['question_text'].'
<br />
<input id='question' type='hidden' value="'.$question_array['id'].'"/>
<textarea name="answer".i></textarea>
</div>';
i++;
}
Now since I know that i starts with 0 and ends when the questions end I can loop through that and access $_POST['answer'.i] and make the insertion.
while ($_POST['answer'.$i]) {
$i = 0;
//insert query after establishing connection, which i believe you got figured out
$sql = 'INSERT INTO ANSWERS VALUES ($_POST['answer'.$i], $_POST['question'.$i])';//Note you must have the question id in some hidden input field
$result = mysql_query( $sql, $conn );//where $conn is your connection object
//after this you might want to do something with the $result,
$i++;
}
I've been searching google to find the code, but I can't find it. What I want to do is pretty simple, but I'm not php expert.
I want to insert multiple cities' name in a single press of submit button (if that is possible)
I have created a database with 2 tables: 'cities' and 'states'
and I have populated the states table
so I need two fields or boxes, one is for states, which is drop down 'select' box which already filled with states name, pulled from the database,
the other one is a cities field which a text type field (type="text").
I want to be able to type in multiple cities name, probably separated with ',' a comma,
and with a single press of submit button, all the value will be inserted into mysql cities table and go on to the next state.
I know I need to create some sort of loop, but I have no idea how to do it.
*index.php (html):
<html>
<body>
<form action="save.php" method="POST">
<input type="text" name ="cities" value="">
<input type="text" name ="state" value="">
<input type="submit">
</form>
</body>
*save.php:
<?php
/* -------include you db connection here ----*/
if(isset($_POST['çities']) && $_POST['çities'] !='' && isset($_POST['state']) && $_POST['state'] !=''){
$cities = explode (',', $_POST['çities']);
$n = count($cities);
for($i=0;$i<$n;$i++){
//Insert data
mysql_query("insert into `cities` (city,state) values ('".$cities[$i]."','".$_POST['state']."') or die(mysql_error());
}
}
?>
First you create the input in the form like this:
<input type="text" name="cities"/>
And you then type: New York, New Jersey, San Francisco, Denver, etc.
Then on the .php file that receives the data (i'd guess it's a $_POST variable), so $_POST['cities'] then, you split it and loop:
// split the values by comma
$_POST['cities'] = explode(',',$_POST['cities']);
// loop the values
foreach($_POST['cities'] as $city) {
// trim (delete white space at the end and start for safety)
$city = trim($city);
mysql_query("insert into `cities` ('city','state') values ('".$city.','".$_POST['state']."');
}
And done.
I'm writting a web app, I'm doing a html form with javascript to validate the data, I'm using a servlet to insert the data in a DB, but I need, when the form load, fill a select in the html form with the rows of a table in a MySql DB, I think I have to make a query in a ResultSet and then fill the select in the html form with this info, and then use the HttpServletResponse, but I have no idea how to make this
Use PHP to access the database and then put it into the form. Something like this:
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM table_name WHERE id='$id'");
$info = mysql_fetch_array($result))
$name = $info['name'];
$email = $info['email'];
$anotherField = $info['another_field'];
In this, the info array has all of the database data in it of the id. But of course you would change the values and stuff. Change table_name to the table name, and if you are going to do something in the URL like ?id=373384 or something, then this will work. (I would assume you are doing it this way.) But, if it isn't 'id', just change WHERE id='$id' to what is in the database to identify a line.
$info['name']; would be the "name" column in your table. But, you can change 'name' and stuff to what you have in your database.
Here is the form code to pre-fill it with data:
<form>
<input type="text" name="name" value="<?php echo $name ?>" />
<input type="text" name="name" value="<?php echo $email ?>" />
<!--etc...-->
<input type="submit" value="Submit" />
</form>
You would need to change a lot of this, but I'm sure you get the point of it. :)
Note: This is not a simple copy+paste script. You will have to do a lot of charing because I don't know what your table values are and stuff.
If you have any questions, please do ask.