I'm building a webpage that allows a user to choose a theme for it. How can I build that based on this suggestion (I have to use SESSION):
<link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['style']?>" />
I've try to make table named 'theme' on mysql database like this:
id | th_name | link
1 | blue | style.css
2 | back | black.css
3 | pink | pink.css
.................
And my code (not work):
<?php
echo "<form>";
echo "<select>";
$sql = "select * from theme";
foreach ($dbh->query($sql) as $row)
{
?>
<option value="<?php $row['link'] ?>"><?php echo $row['th_name'] ?></option>
<?php
}
echo "</select>";
echo "</form>";
?>
The 1st code is the key. Do you have any solution for this? (using jquery or something....?)
Why would you want to use mysql database? You can give a variable the same name css file. And use cookies. This is an implementation example
Just be careful to secure it well!
You could do it the way that you showed above, but for security's sake you would probably want to do it a little different (your way would allow significant opportunity for XSS).
Essentially, you will need to set the $_SESSION global variable to have the style, and that should come directly from the database. If it were me, I would do this:
<?php
echo "<form>";
echo "<select name=\"theme\" id=\"select-theme\">";
$sql = "select * from theme";
foreach ($dbh->query($sql) as $row)
{
?>
<option value="<?php $row['id'] ?>"><?php echo $row['th_name'] ?></option>
<?php
}
echo "</select>";
echo "</form>";
?>
Then, on your postback script, do this:
<?php
if (isset($_POST['theme']) {
// Perform query here to get the link's file, using PDO - watch out for XSS
// Note - you may have to call session_start()
// depending on your php.ini's settings
$_SESSION['style'] = $link;
}
If you are wanting to do this on the fly with jQuery, you can see the information here: How do I switch my CSS stylesheet using jQuery?
In addition, you would want to persist your changes like this:
<script type="text/javascript">
$("#select-theme").on("change", function() {
$.post({
url:'yoururl.php',
data: {
theme: $(this).find("option:selected").value()
});
});
Related
I have a form that takes 56 separate inputs and stores them across 6 tables in mySQL indexed by a unique ID generated when the form is generated. That said, I have an edit mode that passes the UID to the form via the GET method (ie. index.php?editUID=5552d631220810).
My question is, what is the best way to assign all the values to each of the 56 inputs? Currently I am using PHP to process a script line to assign each of the values which are set in the database like this:
<script type="text/javascript" language="javascript">
$(function() {
$("#salesPrice").val("7500000");
});
</script>
But this seems like a cumbersome / not very efficient way to assign my values to their respective fields. Is there a cleaner way to do this? Also, if this is helpful, the names of my mySQL fields are the same as my inputs (ie. salesPrice is the name of the column in my database and the name of the index of my input field if that is useful).
Thanks for any ideas.
You could loop through the column names and the variables and output them to both the HTML and the Javascript
<?php
//MYSQL query goes here
for($results as $data) {
?>
$("#<?php echo $data->name; ?>").val("<?php echo $data->value; ?>");
<?php
}
?>
Or simply just use it with HTML and no javascript
<?php
//MYSQL query goes here
for($results as $data) {
?>
<input type="text" name="<?php echo $data->name; ?>" value="<?php echo $data->value; ?>">
<?php
}
?>
I badly need your help on this.
I can't seem to find what's wrong in my code.
The case is, I am trying to populate my textbox field based on my combobox value and it is connected in sql database. I have tried some codes on the web and then I found a code which seems accurate but I can't seem to display the result on my textbox.
here is my HTML Code:
<?php
echo"Concept Store:";
echo "<select width='100' id='strs' name='strs' >";
echo "<option></option>";
while($row=sqlsrv_fetch_array($stmt))
{
$x= $row['strnm'];
echo " <option> $x</option>" ;
}
echo "</select>";
?>
Address    : <input type="text" id="add" name="add" size="27" /><br><br>
here's the AJAX:
<script type="text/javascript">
$(document).ready(function(){
$('#strs').change(function(){
$.post("gadd.php",{strs:$(this.val() )},function(result){
$("#add").val(result);
});
});
});
</script>
and here's my 'gadd.php'
<?php
session_start();
include ('sqlconn.php');
$gadd=$_post['strs'];
//$t1= mysql_real_escape_string($t1);
$sql="Select distinct dadd1 from ostore where strnm='".$gadd."' ";
$stmt = sqlsrv_query($conn,$sql);
//echo "<option></option>";
while ($row = sqlsrv_fetch_array($stmt))
{
// $type2=$row['dadd1'];
echo $row['dadd1'];
//echo '<option value ="'.$type2.'">'.$type2.'</option>';
}
?>
if you could help me, that would be really really awesome thank you!
Check it. Global variables should be in upper case.
$gadd = $_POST['strs'];
First, make sure you use the proper format for global variables $gadd = $_POST['strs'];
Second, check your query if it is working, try this
$sql=("Select distinct dadd1 from ostore where strnm = '$gadd'");
Most problably it's because you're missing the value in the option.
Use this:
echo " <option value='$x'> $x</option>" ;
instead of this:
echo " <option> $x</option>" ;
If still don't work, like was said before check if the query is working.
You can do that in chrome by the option "Inspect element" and the go to the "Network" tab. You can see what is sent through ajax and the return data.
I'm using ajax post method for send data in my php file. my code is:
<script type="text/javascript" >
ccc = jQuery.noConflict();
function getArticles(catid){
ccc.post('articles.php',{catid: catid},function(data){
ccc('#articlediv').html(data);}
);
}
</script>
and my select list this is:
<select type="list" name="categorylist" onChange="getArticles(this.value)">
<?php
foreach($categoryid as $catid){
$title = modadcatarticlesHelper::getCatById($catid);
?>
<option value="<?php echo $catid ?>"><?php echo $title ?></option>
<?php } ?>
</select>
it's work and show result when select an item but my problem is i want show default items from a default selected item before select an item but my code only show result when select an item.
how can do it?
If you just want to get something going you can simply add the following:
ccc(function () {//run after the page loads and jquery is ready
getArticles(ccc("select[name='categorylist']").val());
});
I would advise you take care of your naming conventions ccc is not very readable and will make the code harder to understand.
I have a PHP script which connects to a MySQL database and creates an HTML dropdown list with data retrieved from it. The script works, I just don't understand how I'm supposed to use it in an HTML form.
The PHP script, named CountryList.php, looks like this:
<?php
function createCountryList() {
$con = mysql_connect("localhost","user","pass");
mysql_select_db('database', $con);
$sql="SELECT Country FROM CountryList";
$result = mysql_query($sql,$con);
echo "<select name=country value=''>Country</option>";
echo "<option value=0>Select Country</option>";
echo "<option value=1></option>";
$curvalue=2;
while($nt=mysql_fetch_array($result)){
echo "<option value=$curvalue>$nt[Country]</option>";
$curvalue = $curvalue+1;
}
echo "</select>";
mysql_close($con);
}
?>
I tried including the PHP file in the head of the HTML page with:
<?php include("CountryList.php"); ?>
And then I tried calling the function which creates the dropdown menu in a form later on:
<form action="insert.php" method="POST">
<label>Which country are you from?</label>
<?php createCountryList(); ?>
</form>
Since nothing happens, I did it wrong. Any advice? Help is appreciated.
EDIT: Bah, I knew it was something silly. The page was an HTML file, PHP didn't process it. Changing the HTML file to PHP solved everything.
What happens when you change this line
echo "<select name=country value=''>Country</option>";
to this
echo "<select name='country'>Country";
Are you sure its in the same folder. Its a good practice to do a check before
if ((include 'CountryList.php') == 'OK') {
createCountryList();
}
else{
echo "Didnt import";
}
Dont do brackets like include()
Try changing echo "<option value=$curvalue>$nt[Country]</option>"; to echo "<option value=$curvalue>{$nt['Country']}</option>";
Echoing out arrays requires curly brackets around the array index if inside a string. Also the index needs quotes or php will assume constant.
This is what I did in one of my HTML forms. Try and see if it works for you.
<select name="country"><?php createCountryList();?></select>
What you do here is on the page where you want the selectbox, you put in this code and call the function.
i am calling a javascript for image gallery and php on a single page.
let me show you some code to make things clear:
PHP
echo "<form method = post action = 'user_submit.php'>";
// get possible answers using question ID
$query = "SELECT aid, atitle FROM answers WHERE qid = '$qid' ORDER BY aid ASC";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_object($result)) {
echo "<div id=captionbox style='width: 110px;float:left;color:#FFF;text-align:center;'>";
echo "<a href='#' class='thumb' ><img class='thumb-img' src='images/roadies/th".$row->aid.".jpg' /> </a>";
echo "<input type = hidden name = aid id = rd".$row->aid." value = ".$row->aid.">".$row->atitle."</input>";
echo "</div>";
}
echo "<input type = hidden name = qid value = '".$qid."'>";
echo "<br/>";
echo "<input type = submit name = submit value = 'Vote!'>";
}
echo '</form>';
}
the above code fetches object ids and places jpeg images accordingly (thumbnails).
now when i click on these thumbnails the javascript opens an overlay to display the large version. i want to pass the value of $row->aid to javascript.
then from the javascript i want to fill out a form and pass the $row->aid and the form to user_submit.php to add it to the DB.
i am new to php. please help me out.
You can write a variable, object or array literal declaration to your javascipt file or to a <script> element in a HTML file, as kiamlaluno correctly said. but i would suggest, (1) use the var keyword and (2) consider using a separate namespace.
If you have several things to tell the JS, it's handy to put them in a PHP hash and json_encode() and then write the encoded string to your output. This takes care of the namespace and correct escaping of the data. For example:
<?php
$jsdata = array(
'this' => "It\"s gone.\nWhat?",
'that' => array(1,3,2),
'another' => 0x2f );
$jsdata = json_encode($jsdata);
?>
<html>
<head></head>
<body>
<script type="text/javascript">
var thedata = <?php echo "$jsdata;" ?>
window.console.log(thedata);
</script>
</body>
</html>
Try that and look at the javascript console to see if it works.
EDIT: Another reason I like this approach is because you can use PHP's array handling conveniences to construct an arbitrarily complex $jsdata while not worrying about escaping issues and only consuming one global JS object name.
First of all, you don't need to echo everything. Something like this is just as valid:
while ($row = mysql_fetch_object($result)) { ?>
<div id=captionbox style='width: 110px;float:left;color:#FFF;text-align:center;'>
<a href='#' class='thumb' ><img class='thumb-img' src='images/roadies/th<?=$row->aid?>jpg' /> </a>
<?php ...
In this case, uses php's echo shortcut tag to echo the value of the contents. It is equivalent to <?php echo $some_variable ?> .
Second, I don't actually see any JavaScript code in your question. But to accomplish what it sounds like you want to do, you can make an HTML form with a SINGLE hidden input whose value is set by selecting one of your images, and then have that form submit to user_submit.php.
The value-setting can happen in a number of ways, which is really up to you. But suppose you wanted it to happen when the user clicked an image. Then you could set the onClick event inside the img tag, like onclick="hidden_input.value='<?=$row->aid?>'"
There are many ways to pass data to JavaScript; the easier is the following:
<script>
aid = <?php print $row->aid; ?>
// The rest of the script
</script>
I only wrote the necessary code, without even write all the attributes for the tag SCRIPT.