html 5 datalist value - html

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

Related

How can i store data from two page in same id in PHP

I have two pages sample4.php and sample5.php where different data is been passed. I need to store both page value in same id currently it stores in different id.
sample4.php
<form action = "sample5.php" method = "GET">
firstname: <input type = "text" name = "firstname" value="<?php echo #$firstname; ?>" />
lastname: <input type = "text" name = "lastname" value="<?php echo #$lastname; ?>" />
nationality: <input type = "text" name = "nationality" value="<?php echo #$nationality; ?>" />
<input type = "submit" value="next" />
</form>
Here i am passing firstname,lastname and nationality to next page sample5.php where extra two fields is added i need to store all value in same id but it actually stores first page value in one id and second page value in other id.Please help me to store both in same. Thanks in Advance
sample5.php
<?php
$conn = mysql_connect("localhost","root","");
$db = mysql_select_db("registration",$conn);
$sql = "INSERT INTO sampletable (firstname,lastname,nationality,mobileno,gender) VALUES ('". #$_GET['firstname']."','". #$_GET['lastname']. "' ,'". #$_GET['nationality']. "','". #$_POST['mobileno']. "','". #$_POST['gender']. "')";
$rep = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form action="sample5.php" method="POST">
<?php if(isset($_GET['id'])) { ?>
<input type="hidden" name="act" value="edit"/>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<?php } else{ ?>
<input type="hidden" name="act" value="add"/>
<?php } ?>
<label> Mobile No </label>
<input type="text" name="mobileno" value="<?php echo #$mobileno; ?>"><br>
<label> Gender</label>
<input type="text" name="gender" value="<?php echo #$gender; ?>"><br>
<input type = "submit" />
</form>
</body>
</html>

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

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

Writing a dropdown form to translate current page; Google thinks I'm translating from English to English

I'm trying to write a dropdown form with a submit button that uses Google translation to translate the current page that I'm on. Here's what I currently have (someone helped me with this):
<form action="http://www.google.com/translate_c" method="get">
<input type="hidden" name="hl" value="en" />
<input type="hidden" name="u" value="<?php echo curPageURL(); ?>" />
<select name="langpair">
<option value="en%7Cafr">English to Afrikaans</option>
<option value="en%7Calb">English to Albanian</option>
...
</select>
<input type="submit" value="Submit" />
</form>
(Echo calls the URL of the current page:)
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
Why does Google think I'm trying to translate from English to English with this code?
I modified your code to the following and it works fine:
<form action="http://www.google.com/translate_c" method="get">
<input type="hidden" name="hl" value="en" />
<input type="hidden" name="sl" value="en" />
<input type="hidden" name="u" value="http://www.stackoverflow.com/" />
<select name="tl">
<option value="af">English to Afrikaans</option>
<option value="sq">English to Albanian</option>
</select>
<input type="submit" value="Submit" />
</form>
I would recheck the curPageURL function and put in the right values for each of the select box items.