Did my best to not post this question but i just can not figure this one out. I'm trying to pass a variable from my html form to a php script the code looks like this so far
<?PHP
echo "<b>Previously Submitted</b><br />";
echo "<select name='files' onChange='updateInfo();'>";
echo "<option value=''></option>";
$files = scandir("businesses");
foreach ($files as $file)
if($file != "."){
if($file != ".."){
if($file != "form.php"){
echo "<option value='$file'>". htmlspecialchars($file) ."</option>";
}
}
}
echo "</select>";
?>
<script type="text/javascript">
function updateInfo(){
<?PHP
mysql_connect("HOSTHERE","USERNAME","PASSWORD");
mysql_select_db("USERNAME");
//This is the line that is of interest i can't figure how to pass document.forms['frm'].files.value to my query
$bizname = mysql_query("SELECT `Value` FROM `" . document.forms['frm'].files.value . "` WHERE `Variable` LIKE 'Name'");
$bizValue = mysql_fetch_assoc($bizname);
echo "document.forms['frm'].businessname.value = '" . $bizValue['Value'] . "';";
mysql_close();
?>
}
</script>
Any help would be greatly appreciated on how i can pass a value from my drop down to the php code.
EDIT
Just to clear things up on what i'm doing sorry about some confusion basically i have a dropdown menu and when the dropdown menu is changed i want to query my database using whatever is in the dropdown as the table in the query and then what ever the value i get from the database is set in my form in a text field. It's all in one page and just trying to update the text filed when the dropdown is changed
The way you have written updateInfo script is not possible. using php concatenation (i.e ".") you can concatenate only php variables not java-script. PHP does not understand it.
what i suggest you is to use ajax. In your updateInfo pass the changed value.
<?php
echo "<select name='files' onChange='updateInfo(this.value);'>";
echo "<option value=''></option>";
$files = scandir("business");
foreach ($files as $file)
if($file != "."){
if($file != ".."){
if($file != "form.php"){
echo "<option value='$file'>". htmlspecialchars($file) ."</option>";
}
}
}
echo "</select>";
?>
and then send ajax and get response. and treat response as you want
<script type="text/javascript">
function updateInfo(val){
$.ajax({
url: '/path/to/ajaxrequest.php',
type: 'post',
data: {'file':val}
success: function(response){
document.forms['frm'].businessname.value = response;
}
});
</script>
and your ajaxrequest.php
<?php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest'){
mysql_connect("HOSTHERE","USERNAME","PASSWORD");
mysql_select_db("USERNAME");
$file = mysql_real_escape_string($_POST['file']);
$bizname = mysql_query("SELECT `Value` FROM ".$file." WHERE `Variable` LIKE 'Name'");
$bizValue = mysql_fetch_assoc($bizname);
$assignVal = $bizValue['Value'];
mysql_close();
echo $assignVal;
}
?>
if you don't get this let me know. thank you.
here i am giving you a link to download code
Put tag inside the HTML Form element..Also I have not understood why you are merging javascript and PHP mysql update function.
yo can write code like
1.Form Element with
Done.
you need to submit the form. you can use post or get as method. but without submitting you can not pass variable from html to php.
<?PHP
echo "<form name='frm_1' id='frm_1' action='your_destination_page.php' method='post>'";
echo "<b>Previously Submitted</b><br />";
echo "<select name='files' onChange='updateInfo();'>";
echo "<option value=''></option>";
$files = scandir("businesses");
foreach ($files as $file)
if($file != "."){
if($file != ".."){
if($file != "form.php"){
echo "<option value='$file'>". htmlspecialchars($file) ."</option>";
}
}
}
echo "</select></form>";
?>
on destination page write this on the top of the page
<?php echo "<pre>";print_r($_POST);echo "</pre>";?>
you will have values of all controls in $_POST array.
<?PHP
echo "<b>Previously Submitted</b><br />";
echo "<select name='files' onChange='updateInfo();'>";
echo "<option value=''></option>";
$files = scandir("businesses");
foreach ($files as $file)
if($file != "."){
if($file != ".."){
if($file != "form.php"){
echo "<option value='$file'>". htmlspecialchars($file) ."</option>";
}
}
}
echo "</select>";
?>
<script type="text/javascript">
function updateInfo(){
<?PHP
mysql_connect("HOSTHERE","USERNAME","PASSWORD");
mysql_select_db("USERNAME");
//This is the line that is of interest i can't figure how to pass document.forms['frm'].files.value to my query
$bizname = mysql_query("SELECT `Value` FROM `" . document.forms['frm'].files.value . "` WHERE `Variable` LIKE 'Name'");
$bizValue = mysql_fetch_assoc($bizname);
$assignVal = $bizValue['Value'];
echo "<script>document.forms['frm'].businessname.value = '" .$assignVal . "';</script>";
mysql_close();
?>
}
Try enclosing in a script tag.
Related
Am a php novice and in need of some help. Basically I have a script that populates a drop down select bar using php. I want to retain the value the user selects in this drop down after submission, so the user does not have to select it again, here is the snippet of the script that I am trying to work with.
<?php
// Start the session
session_start();
$_SESSION["dir"] = $_POST['hiddenVal'];
//echo "hi at last" .$_SESSION["dir"];
?>
<script>
function loadValues()
{
var $x=obj.options[obj.selectedIndex].value;
document.getElementById("hiddenVal").value = $x;
//alert(document.getElementById("hiddenVal").value);
}
</script>
<script>
function country(obj){
//alert(obj.options[obj.selectedIndex].value); //if you want to show in alart
//or put in a variable
var $x=obj.options[obj.selectedIndex].value;
document.getElementById("hiddenVal").value = $x;
alert($x);
}
</script>
<body onload="loadValues();">
<form action="dir20.php" method="post">
<input type="hidden" id="hiddenVal" name = "hiddenVal"/>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
<?php
$path = '/docdownloads';
//echo 'php_'.$abc;
$dirs = array();
// directory handle
$dir = dir($path);
while (false !== ($entry = $dir->read())) {
if ($entry != '.' && $entry != '..') {
if (is_dir($path . '/' .$entry)) {
$dirs[] = $entry;
//echo "$entry</br>";
}
}
}
?>
<select name="country" id="country" onChange="country(this)" required>
<!--<option value="">-----------------</option>-->
<?php
asort($dirs);
reset($dirs);
foreach($dirs as $p => $w):
echo '<option value="'.$w.'">'.$w.'</option>'; //close your tags!!
endforeach;
?>
</select>
<?php
$tex = $_SESSION["dir"];
//$def = "docdownloads";
//$dir = "c:/".$tex;
$dir = "c:/docdownloads/".$tex;
//echo "tex is". "$dir";
echo "</br>";
//if ($handle = opendir('.')) {
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry</br>";
}
}
closedir($handle);
}
?>
You're going to want to check the $_GET var against the selected option. If the current option exists and it has been selected, then give the option the selected attribute.
-----------------
<?php
asort($dirs);
reset($dirs);
foreach($dirs as $p => $w):
$selected = isset($_GET['country']) && $_GET['country'] === $w ? "selected" : "";
echo '<option value="'.$w.'"'.$selected.'>'.$w.'</option>';
endforeach;
?>
</select>
You will need to assign a ID for user if you havent already, then save the submitted option in your database with corresponding ID. Then when you load you'r dropdown box insert a IF clause in your script to check if user has already saved a option.
To achieve that you should save the value the user chose into session
and always when displaying the form, you should check if the value exists in the session and if it exists - display it as selected option.
get the submitted value of country by GET or POST(depend on your form method) and compare it in foreach loop
$countryId = isset($_POST['country']) ? $_POST['country'] : ''; //or $_GET['country'];
foreach($dirs as $p => $w):
$selected = $countryId==$w ? 'selected' : '';
echo '<option value="'.$w.'" '.$selected.'>'.$w.'</option>';
endforeach;
Assuming your form action is on same page, if different, store country value in session and retrieve it from session in this page
Blockquote You need to put your select element inside your form. Otherwise it won't submit
problem solved, script now works, many thanks!
I have a drop down list that generates all files in a folder, which is working. But i would like to only see .jpg files and also i would like to exclude one file from the list as it is a place holder image lets call it "0001_Place_Holder.jpg".
The second part to this is that i want to pick a file from the dropdown list and copy it to a New folder then delete the original image.
this is "move_files_general.php" // which generates my dropdown list
<?php
$dirname = "general_2";
$dir = opendir($dirname);
echo '<form action="move_general.php" method="get">';
echo '<select name="file2">';
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo "<option value=".$file.">$file</option>";
}
}
echo '</select>';
echo '<input type="submit" value="Move To Quality" class="submit" />';
echo '</form>';
?>
This is "move_general.php" // which should copy the file then delete the original
<?php
$dirpath = "general_2";
$dirpath_2 = "quality_2";
$file_to_move = $_GET['file2'];
copy("$dirpath.'/'.$file_to_move", "$dirpath_2.'/'.$file_to_move") or die("Unable to copy");
if (copy("$dirpath.'/'.$file_to_move", "$dirpath_2.'/'.$file_to_move")) {
unlink("$dirpath.'/'.$file_to_move");
if ( unlink ($dirpath.'/'.$file_to_move) ) {
echo $file_to_move . " deleted.";
echo '<script>parent.window.location.reload(true);</script>';
} else {
echo "Error.";
}
}
?>
You would test the filename if its extension is jpg and if it is not equal to your placeholder name.
if(($file != ".") and ($file != "..") and ($file != "0001_Place_Holder.jpg"))
{
if(pathinfo($file, PATHINFO_EXTENSION) ==='jpg'){
echo "<option value=".$file.">$file</option>";
}
}
For the second issue: try to set the folder permissions to 777 for testing purpose. Also echo the strings that you pass to copy(string1,string2) in order to check if something is wrong in there.
First off, Thanks for your answers, and help. Alex Odenthal, that worked for the 1st part. i tried everything to get the 2nd part to work. I finally rewrote it a different way and it's working now, I must have had something wrong , somewhere.
Here is my fixed "move_files_general.php"
<?php
$dirname = "general";
$dir = opendir($dirname);
echo '<form action="move_general.php" method="get">';
echo '<select name="file2">';
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != "0001_Place_Holder_DO_NOT_DELETE.jpg"))
{
if(pathinfo($file, PATHINFO_EXTENSION) ==='jpg'){
echo "<option value=".$file.">$file</option>";
}
}
}
echo '</select>';
echo '<input type="submit" value="Move To Quality1" class="submit" />';
echo '</form>';
?>
Here is my fixed "move_general.php"
<?php
$file_to_move = $_GET['file2'];
$source = "general/$file_to_move";
$dest = "quality/$file_to_move";
copy($source, $dest);
if (copy($source, $dest)) {
unlink($source);
if(file_exists($source)) {
unlink($source); }
else {
echo "Deleted.";
}
}
?>
I have to do a php website for college.
I have to create a password and a submit button. After the I have posted the password I should get text from a html file. What is the best to achieve that?
I tried:
foreach($files as $file2) {
if($_POST['submitPassword']){
if($file2 === '.' OR $file2 === '..' OR $file2 === 'thumbs.db' OR !is_dir($folder.'/'.$file2)) {continue;}
if(file_exists($folder.'/'.$file2.'/doubleindemnity.gif') AND file_exists($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm')) {
echo '<div class="Container">';
echo "<div class='image2'><img src='$folder/$file/doubleindemnity.gif'>";
$lines4 = file($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
$count = count($lines4);
for($a = 0;$a < $count;$a++) {
echo substr($lines4[$a],strlen($folder),strpos($lines4[$a], '.')-strlen($folder));
}
echo "</div>";
}
echo "</div>";
}
}
?>
Help would be highly appreciated:)
Cheers:)
You can easily include the contents of an html file through include(). For example:
include($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
It will call the entire file, and output it (since it's not a PHP file for execution)
I'm currently making a droplist but in the droplist let's say I only want to include only .txt extension files so any other extensions like .php .jpg or any other extensions will not be in in the droplist. How can I do that as simple as possible?
Another question is I want to make a warning IF the folder does not have any .txt extension files an error message will show. So even if there are other .jpg .php or any other files inside as long as there's no .txt file in the folder a warning will show.
Anyone able to give me a hand?
This is what I have done but it only shows a drop-list with no .txt at the end but it will still show other random files in the drop-list though.
if(!(is_dir("./aaa")))
{
die("Must create a folder first, sorry");
}
$lists = scandir("./aaa");
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select>";
foreach($lists as $list)
{
if(($list == ".") || ($list == ".."))
{
continue;
}
echo "<option value=\"";
echo basename($list,".txt");
echo "\">";
echo basename($list,".txt");
echo "</option>";
}
echo "</select>";
echo "</form>";
editted added the substr with $hasTxt
<?php
if(!(is_dir("./aaa")))
{
die("Must create a <strong>aaa</strong> folder first, sorry");
}
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select name=\"aaa\">";
$aaa_files = scandir("./aaa");
$hastxt = false;
foreach($aaa_files as $file_list)
{
if(($file_list == ".") || ($file_list == ".."))
{
continue;
}
if(strlen($file_list)>4 && strtolower(substr($file_list, -4))!='.txt')
{
continue;
}
else
{
$hastxt = true;
echo "<option value=\"";
echo basename($file_list,".txt");
echo "\">";
echo basename($file_list,".txt");
echo "</option>";
}
}
echo "</select>";
echo "<br/><input type=\"submit\">";
echo "</form>";
if($hastxt == false)
{
echo "Must create text files first, sorry";
die();
}
?>
This is what happens for the script that I have now if the folder does not have any txt files.
This is what I actually want if there's no txt file but of course without the arrow
For the first part, just like you continue on directories . and .., you can continue on non-text files:
if(strlen($list)>4 && strtolower(substr($list, -4))!='.txt') continue;
For the warning part, put a flag before the foreach
$hasTxt = false;
And set it to true whenever you get input you don't ignore (ie. after the if(unwanted) continue;)
$hasTxt = true;
Finally, after the foreach check the value of $hasTxt and use it as you prefer.
You could use PHP's substr() function to test the filenames:
if(substr($filename, -3) == 'txt') {
// show file
}
See here: http://php.net/manual/en/function.substr.php
Try this , Hope it will work you
<?php
if(!(is_dir("./aaa")))
{
die("Must create a folder first, sorry");
}
$lists = scandir("./aaa");
$i =0;
foreach($lists as $list)
{
if (strstr($list, '.txt')) {
$i++;
}
}
if($i == 0){
die("the folder does not have any .txt extension files");
}
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select>";
foreach($lists as $list)
{
if (strstr($list, '.txt')) {
echo "<option value=\"".substr($list,0, -4)."\">".substr($list, 0,-4)." </option>";
}
}
echo "</select>";
echo "</form>";
?>
I am pulling out record from the database and inserting them inside a dropdown like this:
echo "<select>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option>" . $drow['id'] . "</option>";
}
echo "</select>";
It works but I need to be able to click on an option on the dropdown and make it link just like:
Record1Here
Record2Here
Record3Here
UPDATE: Latest code:
<script>
function doSomething() {
var currentval = this.options[this.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
</script>
...
echo "<select onchange='doSomething();'>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option value=\"view.php\">" . $drow['id'] . "</option>";
}
echo "</select>";
You can't place anchors on an option within a select list.
What you can do is use JavaScript and then do something on the change event of the select list :
echo "<select onchange='doSomething(this);>';
then in JavaScript do something based on the selected value :
function doSomething(elem) {
var currentval = elem.options[elem.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
Example here
you could update your PHP code to include a value in each option :
echo "<option value=\"urlhere.php\">" . $drow['id'] . "</option>";