php dropdown with another form and submit - php

Hey this problem may look really easy to solve but i can't find any solution, my dropdown isn't saving
<?php
// the relative path to the file
$fname = "Erros1.txt";
$fname2 = "Comentarios.txt";
// read in the file if present
if(file_exists($fname)) $txt = file_get_contents($fname);
if(file_exists($fname2)) $Comentarios = file_get_contents($fname2);
// if the user pushes the submit button
if(isset($_POST["Comentarios"])){
$Comentarios = $_POST["Comentarios"]; // get the entered content
file_put_contents($fname2,$Comentarios); // write the content to the file
}
if (isset($_POST["dropdown"])) {
// cast to integer to avoid malicious values
$dropdown = (int)$_POST["dropdown"];
}
?>
<form method="post" action="#">
<textarea name = "txt" cols = "120" rows = "20">
<?php echo $txt; ?>
</textarea>
<textarea name = "Comentarios" cols = "120" rows = "10">
<?php echo $Comentarios; ?>
</textarea>
// here is the dropdown
<select name="dropdown";>
<?php
for ($x=1; $x<=4; $x++) {
echo '<option value="' . $x . '">' . $x . '</option>' . PHP_EOL;
}
echo $_POST['dropdown']
?>
</select>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
Only my Comentarios Box is saving, maybe i need to change the submit or create another form

Related

passing values from php to html

I want to pass values from my php code (in the same page) to my html - so I could print it out nicely
here is my php function (it's in a while cause it needs to print out 3 lines from a text)
if(sizeof($words)==1)
{
$single = new single;
$docres = $single->find($words);
$dir = "source";
foreach ($docres as $key=>$filename) {
$counter = 0;
$file = $filename +1;
$handle = fopen($dir."/".$file.'.txt',"r");
if($handle)
{
while($counter < 3)
{
echo fgets($handle);
//this is what i need to print in my html
$input = $_POST['input'];
$counter++;
}
}
}
}
and here is my html
<html>
<head>
<title>serach engine</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<form action="" method="POST">
<center>
<h1> My Search Engine </h1>
<input type = 'text' size='90' value='' name = 'search' > <br>
<input type = 'submit' name = 'submit' value = 'Search source code' >
// <input type="value" name="input" value="<?php echo $input; ?>">
</center>
</form >
//I would like to print it out here
</div>
</body >
</html >
I searched over the web and saw a solution using $_POST but it didn't work for me...
You could just append it to a new variable and echo it out in the html.
For example:
while($counter < 3) {
echo fgets($handle);
//this is what i need to print in my html
$input .= $_POST['input'] . "<br />";
// You could also do:
// $input .= "<li>" . $_POST['input'] . "</li> "
// For a list item
$counter++;
}
And then echo it in the html:
</form>
<?php echo $input; ?>
</div>
Note: You did not increment your $counter in the while loop.

How to make elements of an array appear one at the time after clicking submit button?

I have following code:
<?php
include_once 'init/init.funcs.php';
$pollid=(int) $_GET['pollid'];
$questions = array();
$result = mysql_query('SELECT kysimus FROM kysimused where kysimustik_id="' . $pollid . '"');
while($row = mysql_fetch_assoc($result)) {
$questions[] = $row['kysimus'];
}
$count=count($questions);
$x=0;
while ($x<$count){
echo $questions[$x];
$x+=1;
}
?>
<form method="get">
<input type="submit" value="Submit">
</form>
It takes the questions from the database which have kysimustik_id=poll_id and echos them out. But how could I make it work so questions will appear one at the time and new one appears only after I click "Submit" button?
You can do like the following
<div class="row text-center">
<button type="submit" class="btn btn-lg btn-info">Click Here!</button>
</div>
$(function(){
var values = [
"hi",
"hello",
"nice",
"wonderful"
];
var counter = 0;
$(".btn-lg").on("click", function(){
if(values.length) {
counter = (counter + 1) % values.length;
$('.row.text-center').append(values[counter] + ' ');
}
});
});
In the above example I have used javascript array. you can easily replace it by your php array
<?php
include_once 'init/init.funcs.php';
$pollid=(int) $_GET['pollid'];
if(isset($_POST['QuestionID'])
{
$questionID = $_POST["QuestionID"];
HandleAnswer($pollid,$questionID,$_POST['Answer']);
}
else
{
$questionID = 0;
}
$questions = array();
$result = mysql_query('SELECT kysimus FROM kysimused where kysimustik_id="' . $pollid . '"');
while($row = mysql_fetch_assoc($result)) {
$questions[] = $row['kysimus'];
}
function HandleAnswer(string PollId, string QuestionID, string Answer){
// Implement piece of code that sets the answer in the database,
// I leave this up to you since i have no Idea how your database is structured
}
$questionID++;
$count=count($questions);
if($questionID < $count){
?>
<form method="post" action="mypage?=<?php echo $pollid; ?>">
<?php echo $questions[$questionID]; ?>
<input type="hidden" name="questionID" value="<?php echo $questionID;?>"/>
<input type="text" name="Answer"/>
<input type="submit" value="Submit">
</form>
<?php }else{ ?>
THese were all our questions, thank you for answering!
<?php } ?>
I didn't test the code myself, but I'm pretty sure it works. Note that this is a hightly unsafe way to do this, because you can edit the value of the hidden field with extensions such as firebug.

Creating checkboxes in HTML / PHP?

So I have the following code.
My main.html form lets me enter a value (a price for pc) and when I submit it calls the pcs.php script which displays all my pcs under the given value/price.
But I want to have an checkbox beside the information displayed by the script. ie
<br>alienware, 3000, gtx760 checkbox <br> asus rog, 2500, radeodhdxxxx checkbox <br>
and so on).
//main.html
<BODY>
<FORM METHOD="POST" ACTION="pcs.php" >
<INPUT TYPE='textbox' NAME='mypc' VALUE='' >
<INPUT TYPE="SUBMIT" NAME="Button" VALUE="Hit me">
</FORM> </BODY> </HTML>
//pcs.php
<?php
$dd = $_POST['mypc'];
$filename = "computer.txt";
$filepointer = fopen($filename,"r");
$myarray = file ($filename);
for ($mycount = 0; $mycount < count($myarray); $mycount++ )
{
$apc = $myarray[$mycount];
$price = getvalue($apc,1);
$part = explode(',', $apc);
//print $price ."<br>";
//print $str ."<br>";
if ($str <$dd )
{
for($pcount = 0; $pcount<3; $pcount++) {
print $part[$pcount] ."<br>";
}
print "<br>";
}
}
function getvalue ($text, $commaToLookFor)
{
$intoarray = explode(",",$text);
return $intoarray[ $commaToLookFor];
}
// fclose ($filepointer);
?>
<p>
</body>
</html>
// computer.txt file
alienware, 3000, gtx760<br>
asus rog, 2500, radeonhdxxx<br>
alienware, 5000, gtx titan<br>
In pcs.php you'll just need to modify your for loop to look something like:
// Create form
print '<form method="post" action="script_to_post_to.php">';
for ($mycount = 0; $mycount < count($myarray); $mycount++) {
$apc = $myarray[$mycount];
$price = getvalue($apc,1);
$part = explode(',', $apc);
if ($str < $dd) {
for($pcount = 0; $pcount<3; $pcount++) {
print $part[$pcount] ."<br>";
}
// Add checkbox and name with product type
print '<input type="checkbox" name="' . getvalue($apc, 2) . '" />';
print "<br>";
}
}
// Provide form submission button and close form element
print '<input type="submit" value="Submit" />';
print '</form>';
You'll have a form with each element having a checkbox and the ability to respond to the list of checked items in "script_to_post_to.php".

selected value from the listbox in php

I want the value which I select from the listbox and want to store in php variable because I want to use that variable for further use. Please help..
<form name="myform" method="post">
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
</form>
<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">
access that values selectedvalue(val) in parameter
get that selected value and store it in input type= hidden as u need it for further use..
If you want to store into a php-variable, you must post the form. I've added a submit-button below. You also must speciy an action to where the form is submitted to: action="whatever.php"
<form name="myform" method="post" action="whatever.php"> <!-- added action here -->
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
<!-- added submit button -->
<input type="submit" value="ok" />
</form>
Create a php-file called whatever.php and in this file, store the value into $cust_name by doing this:
<?php
$cust_name = $_POST['mylist']; //mylist is the name of the select-list
?>
If you want to post the form without having to reload the page, you must use something called ajax.
This works for me.Try this example.
public function getAptTypesBySelectedKy($valKy) {
$stmt = "SELECT ap_typ_ky, ap_typ_desc FROM apt_types WHERE ap_stat=1 order by ap_typ_desc";
try {
$con = DataHandler::connect();
$values = $con->prepare($stmt);
if ($values->execute()) {
echo '<select class="form-control" name="type" id="apt_types_slc">';
while ($row = $values->fetch(PDO::FETCH_ASSOC)) {
if ($valKy === $row['ap_typ_ky']) {
echo '<option value="' . $row['ap_typ_ky'] . '" selected>' . $row['ap_typ_desc'] . '</option>';
} else {
echo '<option value="' . $row['ap_typ_ky'] . '">' . $row['ap_typ_desc'] . '</option>';
}
}
echo '</select>';
}
} catch (PDOException $ex) {
echo 'Error on apartment types';
$error = $ex;
print_r('<pre>' . $ex->getCode() . '</pre>');
print_r('<pre>' . $ex->getMessage() . '</pre>');
}
}
In your html form page.
$__typKy = $_RA['typeky'] //this line get the selected value from database and set as parameter to the function getAptTypesBySelectedKy()
<?php
$__typKy;
if (isset($_RA)) {
$__typKy = $_RA['typeky'];// you need to find this key from database.
//this is the selected value key of `<select>`
}
$var = new DataHandler();
$var->getAptTypesBySelectedKy($__typKy);
?>

HTML/PHP Survey not passing to MySQL database properly

I'm trying to make a small survey that populates the selections for the dropdown menu from a list of names from a database. The survey does this properly. I want to submit the quote the user submits with this name into a quote database. The quote text they enter into the field goes in properly, however, the name selected from the menu does not get passed in. Instead I get a blank name field.
I understand some of my code is out of context, but the name is the only thing that does not get passed in properly.
On form submit, I include the php file that submits this data to the database:
<form action="<?php $name = $_POST['name']; include "formsubmit.php";?>" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<input type="submit" value="Submit!" />
</form>
The variable $name comes from this (which populates my dropdown menu):
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
echo "</select>";
And here is my formsubmit.php:
<?php:
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
header("Location: quotes.php");
if (#mysql_query($sql)) {
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
Your form action stuff looks weird, but regardless, I think the problem you're having has to do with not setting $name = $_POST['name'] like you're doing with $quotetext = $_POST['quotetext']. Do that before the sql statement and it should be good to go.
edit to try to help you further, I'll include what the overall structure of your code should be, and you should tweak it to fit your actual code (whatever you're leaving out, such as setting $query for your name options):
file 1:
<form action="formsubmit.php" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<select name='name'>
<?php
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
?>
</select>
<input type="submit" value="Submit!" />
</form>
formsubmit.php:
<?php
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$name = $_POST['name'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
$nyme = htmlspecialchars($temp['name']);
echo "<option value='$nyme'>$nyme</option>";
}
echo "</select>";-
This way you will receive the value of the name in $_POST array
and you have to get that value out of $_POST array as well you need to change the
code add the following line to get the name in your script.
$name = $_POST['name'];
you need to change the form action tag
<form action='formsubmit.php' .....>
and in that file after successful insertion you can redirect the user to whereever.php.
so it was fun explaining you every thing bit by bit change this now in your code as well.
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}

Categories