cinek 0 Newbie Poster

I'm alowing a user to save their "configuration" from a form to the database> I also want the user to be able to load that configuration back onto the form from the database. Now tbh I've got no idea how to do it. I'd like this to be done without refreshing the page.

This is the php I've got so far. I've got no idea how to pass the data to jquery

<?php

    
    
    $Name = $_REQUEST['configName'];    
    
    $connect = mysql_connect('localhost', 'root', 'pass') or die (mysql_error());
    
    mysql_select_db('ipsum') or die (mysql_error());
    
    $query = "SELECT * FROM con WHERE Name = '1234'";
    
    $sql = mysql_query($query) or die (mysql_error());
        
    while($row = mysql_fetch_array($sql)){
        
        $Sections = $row['Sections'];
        $Fontcolor = $row['Fontcolor'];
        $Backgroundcolor = $row['BackgroundColor'];
        $Font = $row['Font'];
        $Fontsize = $row['FontSize'];
        $Lineheight = $row['LineHeight'];
        $Letterspacing = $row['LetterSpacing'];
        $Fontstyle = $row['FontStyle'];
    }
    
    mysql_close($connect);
?>

I'm using this php & jquery/ajax to submit the data - maybe it's possible to modify these two so that I can use them to retrieve the data?

<?php

function report($err){
    $date = date("d/m/Y G:i:s");
    $fp = fopen('log.txt', 'a+');
    fwrite($fp, "[" . $date . "]" . "$err\r\n");
    fclose($fp);
}

$sections = $_REQUEST['sections'];
$fontColor = $_REQUEST['fontColor'];
$bgcolor = $_REQUEST['bgcolor'];
$font = $_REQUEST['font'];
$fontSize = $_REQUEST['fontSize'];
$lineHeight = $_REQUEST['lineHeight'];
$letterSpacing = $_REQUEST['letterSpacing'];
$fontStyle = $_REQUEST['fontStyle'];
$configName = $_REQUEST['configName'];

$connect = @mysql_connect("localhost", "root", "pass");

if($connect == false) {
    $err = " Unable to connect to db: " . mysql_error();
    report($err);
    header("HTTP/1.1 401 Unauthorized");
    exit;
}

mysql_select_db("ipsum") or die ("doesnt exist: " . mysql_error());

$update = "INSERT INTO con (Sections, Fontcolor, BackgroundColor, Font, FontSize, LineHeight, LetterSpacing, FontStyle, Name) VALUES ('".$sections."', '".$fontColor."', '".$bgcolor."', '".$font."', '".$fontSize."', '".$lineHeight."', '".$letterSpacing."', '".$fontStyle."', '".$configName."')";

$query = @mysql_query($update);

if($query == false) {
    $err = " Unable to update: " . mysql_error();
    report($err);
    header("HTTP/1.1 303 See Other");
    exit;
}

mysql_close($connect);


?>
// JavaScript Document

$(function(){
$('.save').click(function(){
    
    var sections = $('#sections').attr('value');
    var fontColor = $('#fontColor').attr('value');
    var bgcolor = $('#bgcolor').attr('value');
    var font = $('#font').attr('value');
    var fontSize = $('#fontSize').attr('value');
    var lineHeight = $('#lineHeight').attr('value');
    var letterSpacing = $('#letterSpacing').attr('value');
    var fontStyle = $('#fontStyle').attr('value');
    var configName = $('#configName').attr('value');
    
    //Datastring to pass through                  
    var dataString = {sections:sections, fontColor:fontColor, bgcolor:bgcolor, font:font, fontSize:fontSize, lineHeight:lineHeight, letterSpacing:letterSpacing, fontStyle:fontStyle, configName:configName};          
    
    
    // Check if config name is empty
    if(configName == "")
    {
        $('#errorMessage').append("<img src='images/error.png' id='errorImage' height='48' width='48' /><h2>Podaj nazwe konfiguracji</h2>").fadeIn("slow").delay(1000).fadeOut(2000,(function(){$(this).empty()}));
    }
    else {
                  
    $.ajax({
type: "POST",
url: "update.php",
data: dataString,
datatype: "xml",
success: function(){$('#successMessage').fadeIn("slow").delay(1000).fadeOut(2000);},
error: function (){$('#errorMessage').append("<img src='images/error.png' id='errorImage' height='48' width='48' /><h2>Wystapil Blad!</h2>").fadeIn("slow").delay(1000).fadeOut(2000);}

        
    });
    }
    return false;
    
});
});

edit: I already have values in the form so I don't want to add a new one, I want to select it. The form consists of 6 select lists & 2 textboxes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.