What I would like to do is find a way to "hide" the option value listing from the page source if possible.
I have successfully created a page with multiple drop downs (6 now, will be 15+ in total); database queries are working, drop downs are working, posting selections and results are visible in a separate processing page, HOWEVER;
The option values are showing in the page source.
Page Design (for this example)
test.php - contains db connect (will change that to separate include_once later), queries, option values code, and table data.
index.php - contains only an include statement calling test.php.
Running test.php in a browser exposes queries (which I don't want) and loading the index that is including test.php hides queries, but shows options which I prefer not to have.
I have searched everywhere (maybe I am not asking the question properly) and have tried to run the sql in one page and posted the result to another, but I cannot get it to work.
What I wanted to do:
Run MySQL queries in a separate "function" page and then call the output (select name) to another (HTML) page and embed the dropdowns into a cell of a table.
Questions:
Is it possible to hide the option values in the index.php page?
Is this my design flaw and if so, what is a better way?tem
How do I run queries in one page, and then call the output of that query as a drop down into an HTML page?
test.php (Shortened example)
<!--mo-->
<td colspan="2"><div align="right"><font face="Arial, Helvetica, sans-serif">
<?php
$query = "SELECT date_code_mo, date_code_mo_desc FROM tbl_gm_assy_date_code_mo ORDER BY date_code_mo ASC";
echo '<select name="date_mo">';
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['date_code_mo_desc'] . '">' . $row['date_code_mo'] . '</option>';
}
mysqli_free_result($result);
}
echo '</select>';
?>
</font></div></td>
<!--week-->
<td colspan="2"><div align="left"><font face="Arial, Helvetica, sans-serif">
<?php
$query = "SELECT date_code_wk, date_code_wk_desc FROM tbl_gm_assy_date_code_wk ORDER BY date_code_wk ASC";
echo '<select name="date_wk">';
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['date_code_wk_desc'] . '">' . $row['date_code_wk'] . '</option>';
}
mysqli_free_result($result);
}
echo '</select>';
?>
index.php source (table definitions not shown)
<select name="date_mo"><option value="Janurary">01</option><option value="Feburary">02</option><option value="March">03</option><option value="April">04</option><option value="May">05</option><option value="June">06</option><option value="July">07</option><option value="August">08</option><option value="September">09</option><option value="October">10</option><option value="November">11</option><option value="December">12</option></select>
<select name="date_wk"><option value="First">A</option><option value="Second">B</option><option value="Third">C</option><option value="Fourth">D</option><option value="Fifth">E</option></select>
I have used a lot of examples available on here to get this far, and I am sure it is something small that has been missed, regardless, THANKS TO YOU ALL!
I'm not sure if I understand your question..
1)
I don't understand why you are worried about option values in the page source, they are simply a value send to the server. Where they are interpreted and get their meaning. You can change them to anything you want. Only the client side code (where the option tags appear) is visible for the user, the serverside code is not visible for the user. --> the user can see the values of the option tags, but has no idea what you are going to do with them serverside.
2) see 1)
3) when submitting a form, the values are send to the url specified with the action attribute:
<form action="evaluate.php" method="post">
...
</form>
The output of evaluate.php will be send to the client --> this is the next page the user will see. For the syntax to form dropdown menu's check here:
Pre-filling select tags from array
For retrieving the values send, have a look at the:
$_GET['varname'] or $_POST['varname'] arrays. Depending on the method specified in the form tag, the values send are stored here.
Where varname = the name attirbute of the form element. Depending on the type of the formelement it is more or less trivial to extract the values. There are plenty of tutorials arround and very easy to find.
I also recommend having a look at the following topic:
How can I prevent SQL injection in PHP?
You're looking for cascading dropdowns, it's the best way to manage it in your case. This question is already answered here:
How to make a Cascading Drop Down List in PHP using jQuery
It uses a classic example of selecting cities based on a selected country, but you can easily adjust it for yourself, by adding .change() function for all of your dropdowns except the last one and adding a php script that returns the right list.
There's also a plugin for JQuery, if you want to check it out:
jQuery Cascading Dropdown Plugin
Related
self taught at PHP so please spare me if i made any obvious errors,
im trying to dynamically create an accordion and have the corresponding content beneath each accordion header be created from its corresponding table,(if the header is tacos, the information below would come from the tacos_info table) some of the solutions i came up with im not sure about, cheifly passing a variable value as the table name in the mysqli query.
<?php
//initialize list
$res = mysqli_query("SELECT * FROM tables ORDER BY votes DESC");
//build the accordion header and div content in descending order
while($row= mysqli_fetch_assoc($res)){
//create value for SQL table name to build content
$dbname='$row['name']';
//create the accordion headers
$accordioncontent= '<h3>'.$row['name'].'</h3>';
//build the query that will be used to create the accordion content dynamically
$res2 = mysql_query("SELECT * FROM '$dbname' ORDER BY votes DESC");
//while loop to build the div content dynamically
while($row2= mysqli_fetch_assoc($res2)){
//dynamically create the list items i.e the accordion content
$ranks= '<li id="li $row2['id']">
<div class="tut-img">
<img src="<?php echo $row2['img']?>" width="50" height="70" alt="<?php $row['title']?>" />
</div>
<div class="title"><?php $row2['title']?>
</div>
</li>';
}//close content while loop
//create the submit button that submits according to each accordion divs content
$submitbutton='';
//limit submissions to once per IP, per table
$voted=false;
$vcheck= mysqli_query("SELECT 1 FROM sort_votes
WHERE ip='".$_SERVER['REMOTE_ADDR']."'
AND date_submit=CURDATE()
AND Tablename='$dbname' "
);
if(mysqli_num_rows($vcheck)==1)
$voted=true;
//conditional to assign either a submit or edit
if(!$votedIFC){$submitbutton='Enter opinion<span></span>'}
else{$submitbutton='Enter opinion<span></span>'}
//concatonate the div header with the div content
$accordioncontent .= '<div><ul class="sort" id="rank_ul">'$ranks'</ul><div class="button-holder">'$submitbutton'</div></div>';
}//close accordion while loop
//send all this data to the AJAX GET request
echo $accordioncontent;
?>
am i passing these values to the query correctly? is this allowed? if not what are better alternatives? any and all tips, input and knowledge is much appreciated.
First of all, I can see that you are using simple and double quotes sometimes in a strange way. Which IDE are you using for your development? My first recommendation, would be for you to use a proper editor that will automatically check your code syntax.
For example this seems strange (difficult to ready and will surely at one point generate an error):
'<li id="li $row2['id']"> ..... ';
In my opinion, a correct syntax here would be:
"<li id='li ".$row2['id']."'> .... ";
More details here: http://www.trans4mind.com/personal_development/phpTutorial/quotes.htm
Now to answers to your question, you seems to be more focused on the way you are passing the values to the SQL query. If you are looking for best practices, then I'd recommend you to use php PDO. It's a layer between PHP and your database that will among other things, make sure that the you correctly passed values to SQL (reduce risk of SQL injection, problem of quotes, etc ...).
More details about PDO here: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
My last advises:
Please make efforts in the indentation of your code, it will be easier to read and understand for you and others and it will then avoid obvious errors.
Use correct variable name. For example you are here using $dbname to speak about database tables ...
Good luck.
I am trying to write a wordpress plugin and I have hit a bump. I am new to PHP (coded in Java before) and javascript so I am not sure whats the best way to solve my problem.
The Background
I have some data in a mySQL DB that I am using (each row has a unique ID and some other information I have added). I am able to search the DB using
$headss = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}costumesdb WHERE location = 'head'", ARRAY_A);
And display some of the information to the user using (this is one of 5 different drop-downs but they are all created in the same way)
Head: <select name="head">
<?php foreach ($heads as $head) { ?>
<option value="<?php echo $head['pieceName'] ?>"><?php echo $head['shopName'] . " - " . $head['pieceName'] ?></option>
<?php } ?>
</select>
For the moment I want the user to be restricted to choosing information that is already in the system.
The problem
The DB contains 2 pieces of information that the user does not need to know to fill in the form (a website URL and a picture URL). I need these 2 pieces of information once the form is submitted (I need to write some more code for that) to the server which spits out another page with the 2 URL's in it.
Whats the best way to send the data back to a PHP script? Am I able to access the row of data that the user has selected in the drop down and send the unique ID for that row back or do I need to do something else?
Edit:
This is the script that I am using to submit the code:
$('#createacostume').form({
success:function(data){
$.messager.alert('Info', data, 'info');
}
});
'
And then the page to display the information returned is:
$cname = $_POST['cname'];
$head = $_POST['head'];
echo "Data Returned Name $cname head $head
I think this is what you are asking:
User has to choose an item from a drop down and submit a form. You have to display the website URL and the image for that item in a second page. You want to know how this is typically accomplished.
If that's the case, you should pass the row id of the item to the second page like so:
<option value="<?php echo $head['ROW_ID'] ?>"><?php echo $head['shopName'] . " - " . $head['pieceName'] ?></option>
Then use the ROW_ID in the second page to access the data from the database and print out the website URL and the image.
Submit the first form (without the two field), INSERT the data into the database, get the ID of insert.
Pass the ID to the next page which would set the ID into a hidden form field (or GET or POST parameter, plenty of choices) of the new form (with the two fields and just UPDATE the database upon submitting the second form.
If you like to show the original data in the second form, just pull the data from the database and use it to render the form instead of passing just the ID into a hidden field.
The $i variable is each field and is populated in the 'inp' boxes which are just input boxes and sboxes which are just select boxes. There is only one form when the page loads and it has all the criteria for a trainer to be added. The trainer name would be trainer_name1 on the first form. If they chose to hit the new button they could fill out the information for another trainer, the input box for the second form for 'name' would just be trainer_name2 and all the other fields are named respectively to what they are in the form. As new forms are built in just adds the next consecutive number onto the end of whatever the field might be named.
Here is my code:
<fieldset><legend>Trainer Request</legend></fieldset>
<tr><td><input type='button' onClick="if (show_item(1,10, 0)) { this.style.display = 'none'; }" value='New'></td></tr>
<?php
$contact_array = array('ACCEPTED TRAINING','DECLINED TRAINING','LEFT MESSAGE FOR TRAINING ACCEPTANCE',
'NEED TO CONTACT TO SEE IF INTERESTED',
'NEED PAPERWORK/TRAINING',
'NEED SIGNED CONTRACT AND PAPERWORK',
'NEED TO COMPLETE TRAINING');
for ($i = 10; $i > 0; $i=$i-1)
{
echo "<table id='hidden$i' style='display:none;'><tr>";
echo "<td>Date</td><td>Status</td></tr>";
echo "<tr><td>"; inp("date$i"); echo "</td><td>";
sbox("contact$i", $contact_array, 0, 'wide2');
echo "</td></tr>
<tr><td>Facility</td><td>";
inp("facility$i",50); echo "</td></tr>";
echo "<tr><td>Trainer Name</td><td>";
inp("trainer_name$i",35);
echo "<tr><td>Distance From</td><td>";
sbox("distance_from$i", array('1','2','5','10','15','20','25','30','40','50','60','70','80','90','100'));
echo "</td></tr>
<tr><td>Phone</td><td>";
inp("phone$i",13,'phone');
echo "</td><tr><tr><td>Email</td><td>";
inp("email$i",50);
echo "</tr><tr><td>Address 1</td><td>";
inp("addr1$i",50);
echo "</tr><tr><td>Address 2</td><td>";
inp("addr2$i",10);
echo " City ";
inp("city$i",20);
echo "</td></tr><tr><td>State</td><td>";
inp("state$i",2);
echo " Zip ";
inp("zip$i",'zip');
echo "</td></tr><tr><td>Notes</td><td>";
tbox("notes$i", 40, 3);
echo "</td></tr></table>";
}
?>
<script type='text/javascript'>
show_item(1,10,1);
</script>
As you can see down here I'm building a link which would name the link whatever the trainer name is, in this case trainer_name1 is Tim Jackson, so i've just built a hyperlink with his name.
<?php
// print_r ($_GET);
echo sendback_link($_GET['trainer_name1'], 'ACS/TrainerLookup', 'trainer_id=trainer_code&trainer_name=trainer_name');
?>
I'm confused on how to add a dynamic link like this into the for loop so as the form builds 1 - 10 each trainer_name2, trainer_name3, trainer_name4 etc etc. will have their names hyperlinked.
I'm thinking I create a new variable for the number 1-10 and append it onto the $_GET[trainer_name$].. something like that?
I hope that makes sense and any help would be greatly appreciated.
If I understood your question correctly, you are trying to add more content or replace content on a webpage that has already been generated with PHP.
I think what you are trying to do can be achieved using AJAX.
AJAX is a technique for creating dynamic webpages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
In your case, the process can be described in 3 steps:
the browser making a new request to your server (using javascript)
the server processes the request and sends some information back to the browser
the browser deals with that information (again using javascript) and then updates part of the webpage
There is an example here that shows how you can dynamically change the webpage contents.
Although, if you are allowed to, I would suggest that you use a javascript framework like jQuery which simplifies the whole process (google it for download and instructions on how to use).
You can read about using AJAX with jQuery here and reading the examples that follow to better understand how you can use it.
If you are going to try and use AJAX with jQuery or simply AJAX I suggest that you try it on a test page to get a simple working example, then adding a few things and checking to see if everything works as expected. When the test page is working as expected, import the code to your page.
This is how I would do it, there may be better or easier ways to do it.
I would like to know if it would be possible to run an update query when an item is selected from a drop down list. The user makes a choice, a function is then called to update a particular field in a database. This will be achieved using a select box to store the options. Thanks.
echo '<td>';
echo '<select name="order_status[]" onChange = "update()">';
echo '<option value = "1" class ="pending">Pending</option>';
echo '<option value = "2" class = "approved">Approved</option>';
echo '<option value = "3" class ="disapproved">Disapproved</option>';
echo '</select>';
echo '</td>';
echo '</tr>';
Yes, it's possible. If you want the query to be run seamlessly (that is, without a submit button being pressed and the page refreshing), then you'll need to use Ajax to send the request asynchronously to your PHP script.
EDIT: The easiest thing to do is simply use jQuery's $.get() functionality in your onchange event. That way, each time someone chooses an option, jQuery will send the request to your PHP script with that option's value. The PHP script will run, and return the new data back to your jQuery, which will then use its DOM functionality to insert that data into your page.
You can do the same thing with a button. Just stick $.get() in the button's onclick event rather than in the select element's onchange event.
The jQuery site's documentation will give you relevant code examples.
EDIT 2: Okay, here's a very canned example.
First, let's think about the actual process you want to have happen on the back end. In the simplest terms, you want to take an id from user input and use that to run in a query. Pretty straight forward (using PDO for the database work):
// in a real app, you'd need to sanitize and validate the incoming id
$id = $_GET['id'];
$stmt = $dbh->prepare("SELECT * FROM table_name WHERE id = :id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($row); // makes it jQuery friendly
Okay, so the back end is pretty simple. Now, for the font end, where the magic happens. Here's how I'd approach using both a select element and a button in order to pass the id back to the PHP script, and then handle the results:
<!-- Your HTML up to where the select and button are on your page -->
<select id="id" name="id">
<option value="1">Something</option>
<option value="2">Something Else</option>
<option value="3">Yet Another Thing</option>
</select>
<button id="btn" />
<!-- In your jQuery -->
$("#btn").click(function() {
$.get("path/to/your/back/end/script.php", { "id" : $("#id").value }, function(data) {
/* the data will be the json_encode($row) that was echoed from the PHP script.
* so, you'll need to drill into it, take the data you want, and use
* jQuery's/JavaScript's DOM manipulation tools to insert the data on your
* page
*/
}) // close $.get()
}); // close .click()
None of this is tested, and it's admittedly incomplete, but that should be more than enough to get started. Really, all you'll need to figure out is how to drill into the returned data (it's a JSON object, so it shouldn't be too bad... use a browser's web development tools to see how the data is actually formed) and insert it where you want. That, and any dumb errors I may have made above.
Hope this helps!
I do not know if it makes sense but i am trying to echo a hidden field in select box but does not work. How can i echo
echo "<input name='testt' type='hidden' id='testt' value='".$ver["cats_fee"]."'>";
in here:
<select size="1" name="parentcat">
<option value='0'>---- Top Category ----</option>
<?
$al=mysql_query("select * from cats where cats_parentid='0' order by cats_id desc");
while($ver=mysql_fetch_array($al))
{
echo "<option value='".$ver["cats_id"]."'>".$ver["cats_name"]."</option>";
$al2=mysql_query("select * from cats where cats_parentid='".$ver["cats_id"]."' order by cats_id desc");
while($ver2=mysql_fetch_array($al2))
{
echo "<option value='".$ver2["cats_id"]."'> > ".$ver2["cats_name"]."</option>";
}
}
?>
</select>
It looks like you're trying to send multiple pieces of data based on a user-selection. It's easier just to send the unique id (as you are already doing) then lookup the 'fee', along with any other data you need' in the script that the form sends to.
As an aside, it also looks like you are making many database calls to, effectively, get all cats back. If that's true, it's probably more efficient to just get them all with a single call and handle the hierarchy in your script.
Because both the select box, ancd an input field are form elements, you can't nest them. But because you only use hidden input fields for storing data on your page, out of sight for the user, it doesn't really matter where you place it, as long as it's inside the form.
You can't see which option the user is selecting on the client side when you are at the server side. The solution is using javascript - which is a client side language, search like this or this would help alot I think.