-
How to return data after a refresh before you post data
I have decided to put something up that AJAX didn't help with. I have search for several days for a solution and decided to do it myself.
Imagine you want a chain select from a database but all you find is XML, TXT, PHP with java and AJAX but you can't use the database.
The solution is simple, well i wish i knew then but its a learning curve i suppose. Using PHP start at the following:
<PHP code>
$html = "";
//country select tag
if(isset($_GET['cid'])) {$cid = $_GET['cid'];} else {$cid = 0;}
$con = mysql_query("select * from country order by country");
$html .= "Country: <select class=register id=country onchange=state(this.value) name=country>\n";
$html .= "<option value=''>Please Select An Option</option>\n";
for($co = 1; $co <= mysql_num_rows($con);$co ++)
{ $cou = mysql_fetch_array($con); $html .= "<option " . ($cid == $cou[0] ? "selected " : "") . "value=$cou[0]>$cou[1]</option>\n"; }
$html .= "</select><input type=\"hidden\" id=\"country_hidden\" name=\"country_ID\">\n";
//county / state select tag
$html .= "\nCounty :\n";
$html .= "<select id=county class=register name=county>\n";
$ste = mysql_query("select * from state where cid=$cid order by state");
for($st = 1; $st <= mysql_num_rows($ste);$st ++)
{
$stt = mysql_fetch_array($ste);
$html .= "<option value=$stt[0]>$stt[2]</option>\n";
}
$html .= "</select>";
$html .= "\n";
<PHP code>
<Javascript code>
function setcookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
var cookie_string = name + "=" + escape ( value );
if ( exp_y )
{
var expires = new Date ( exp_y, exp_m, exp_d );
cookie_string += "; expires=" + expires.toGMTString();
}
if ( path )
cookie_string += "; path=" + escape ( path );
if ( domain )
cookie_string += "; domain=" + escape ( domain );
if ( secure )
cookie_string += "; secure";
document.cookie = cookie_string;
}
function getcookie ( cookie_name )
{
var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
if ( results )
return ( unescape ( results[1] ) );
else
return null;
}
function state(values)
{
if (document.getElementById('country').selectedIndex = 0)
{
alert('No country was selected');
}
setcookie('username', document.getElementById('username').value);
setcookie('fname', document.getElementById('fname').value);
setcookie('lname', document.getElementById('lname').value);
setcookie('address', document.getElementById('address').value);
setcookie('city', document.getElementById('city').value);
setcookie('telephone', document.getElementById('telephone').value);
setcookie('email', document.getElementById('email').value);
alert(document.getElementById('username').value + " if you have entered data after this page it mite not have been saved");
window.location = "?cid=" + values;
}
function onload ()
{
document.getElementById('username').value = get_cookie('username');
document.getElementById('fname').value = get_cookie('fname');
document.getElementById('lname').value = get_cookie('lname');
document.getElementById('address').value = get_cookie('address');
document.getElementById('city').value = get_cookie('city');
document.getElementById('telephone').value = get_cookie('telephone');
document.getElementById('email').value = get_cookie('email');
}
if you are using the onload function place the onload fuction above within your current onload function
and create your own inputs to test it, can't be bothered to do that its late and i have to go into bed.
-
-
Using Javascript to set cookies when you have a server-side language available is a pretty bad idea.
-
-
if you can suggest another way hopefully without a refresh that would be fab as the method i have used doesn't work in Firefox which is critical to me. if any one can help give me a better solution i will be greatfull
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks