1

in my html i have:

<form method = "post" action = "creablog.php">
<p id = "titolosfondo"> Sfondo </p>
        <select id = "sfondo" name = "sfondo" >
            <option hidden></option>
            <option value = "green"> Verde </option>
            <option value = "blue"> Blu </option>
            <option value = "red"> Rosso </option>
        </select></br>
<input type = "submit" id = "crea" name = "crea" value = "Crea Blog" />
</form>

I have a form, where inside there is a SELECT OPTION, where I can select a color. When I click the button I would like the background color to be saved on another page.

i made this function in jquery:

$(document).ready(function(){
     
    function cambiosfondo()
{   
    var x = document.getElementById("sfondo");
    var bgcolor = x.options[x.selectedIndex].value;
    document.body.style.backgroundColor = bgcolor;
}
});

in the database I have a table called BLOG where I have the SFONDO attribute. this is the query I use in the CREA BLOG page to insert the data.

$query2 = "INSERT INTO blog (titoloBlog,nomeSottotema,nomeUtente,sfondo, font, colorefont) VALUES ('$titoloblog','$sottotema',(SELECT nomeUtente FROM utentiregistrati WHERE nomeUtente = '$nomeutente'),'$sfondo','$font','$colore');";
            $result2 = mysqli_query($mysqli, $query2);
            
            if(!$result2){ 
                echo 'errore 2';
            } else {
                header("Location: blog.php?blog=$titoloblog");
            }

this is the query I use on the BLOG page to get the data. The BLOG page is the page where I want the background to change color.

$blog = $_GET['blog'];
$query = "SELECT * FROM `blog` WHERE titoloBlog = '$blog'";
    $result = mysqli_query($mysqli, $query);    
    
    if(!$result){
            echo 'errore';
    }

no error appears. the INSERT query executes successfully. But there is no background change on the BLOG page. please help me!

ymod
  • 11
  • 2
  • 2
    Which page? Are you talking about a temporary or a permanent change? – El_Vanja Dec 06 '20 at 15:43
  • 2
    _"to be saved on another page"_ - You need to explain that in detail. It's too vague, unspecific and unclear at the moment. Save it how? For everyone? Just that user? Just that session? Do you want to store/read it using databases? Generate CSS? Generate HTML? – M. Eriksson Dec 06 '20 at 15:44
  • I'm talking about permanent change. this background color I choose must be saved on the other page. that is, if I choose the red color, when I reopen that page I want the background to always be red. – ymod Dec 06 '20 at 15:52
  • You need to store that fact somewhere on the server then, e.g. in a database perhaps, and then in the place where you want to use it, you need to load the value from the place you stored it – ADyson Dec 06 '20 at 15:57
  • I have a database, with a table called BLOG where the background attribute is present. In this page which is called CREATE BLOG I have an INSERT INTO query to insert the background, and it actually inserts it. Then on the page where I want the background to appear, which is called BLOG, I have another SELECT query which selects the background value. But I don't have any graphical changes. The background of the BLOG page does not have the color I have selected. – ymod Dec 06 '20 at 16:00
  • 2
    Then it seems like you have most things in place. So what is your _actual question_? Where are you stuck? It's obviously not with the posted code. Please edit your question to be about your real problem/issue. Post all relevant code, example data, expected result and what currently happens. – M. Eriksson Dec 06 '20 at 16:00
  • I edited my question, please can you take a look – ymod Dec 06 '20 at 16:09
  • Please note that the way you're building your query is unsafe. You're open to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). You should use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) or [PDO](https://www.php.net/manual/en/book.pdo) instead. – El_Vanja Dec 06 '20 at 16:54

0 Answers0