/    Sign up×
Community /Pin to ProfileBookmark

Ajax OnBlur & PHP

Hello,

I have a very simple page that has an “onblur” that calls an ajax function.

Once the value from that select object when changed the ajax function calls a php page that fetchs the data from SQL and returns new HTML code with a new drop-down menu (select object).

once I post or even get the values the returned value (which is visible on the page, doesn’t get forwarded into the post / get).

When I post these are the values I get:

Array (
[country] => 1
[cname] => somestate
[AddCity] => Add City
)

Here is the code for all of it:

First PHP Page:

[code]
<?php
session_start();
if (!isset($_SESSION[‘login’])) {
header(“location: login.php”); exit;
}
global $DB, $INFO;
require (‘conf/db.class.php’);

$DB = new dbCon;
$DB->link[‘dbname’] = $INFO[‘dbname’];
$DB->link[‘dbuser’] = $INFO[‘dbuser’];
$DB->link[‘dbpass’] = $INFO[‘dbpass’];
$DB->link[‘dbhost’] = $INFO[‘dbhost’];

$DB->dbConnect();

$mode = $_GET[‘view’];

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<meta http-equiv=”imagetoolbar” content=”no”>
<meta name=”distribution” content=”global” />
<meta name=”revisit” content=”10 days” />
<meta name=”revisit-after” content=”10 days” />
<meta name=”resource-type” content=”document” />
<meta name=”audience” content=”all” />
<meta name=”rating” content=”general” />
<meta name=”robots” content=”all” />
<meta name=”robots” content=”index, follow” />
<meta name=”language” content=”en” />
<meta name=”country” content=”US” />
<title>Add City</title>
<script type=”text/javascript” src=”<?=$INFO[‘siteaddr’]?>scripts/selectstate.js”></script>
</head>
<body bgcolor=”#FFFFFF” text=”#000000″>
<center>
<table width=”300″ cellpadding=”0″ cellspacing=”0″ align=”center”>
<tr><td align=”center”><br />
<? if ($mode == “window”) {
if($_POST[‘AddCity’] == “Add City”) {
$country = $_POST[‘country’];
$states = $_POST[‘state’];
$city = $_POST[‘cname’];
print_r($_POST);
} // Close Post ?>
<fieldset>
<legend>Add City</legend>
<table cellpadding=”0″ cellspacing=”0″ align=”left”>
<form name=”AddCities” method=”POST”>
<tr>
<td>Country:&nbsp;</td>
<td>
<select name=”country” style=”width: 146px” onblur=”showState(this.value);”>
<option value=”0″ selected=”selected”>&nbsp;</option>
<? $sql = “select * from `countries` ORDER BY `sortorder`”;
$result = $DB->query($sql);
while($row = mysql_fetch_array($result)) { ?>
<option value=”<?=$row[0]?>”><?=ucwords($row[1])?></option>
<? }?>
</select>
</td>
</tr>

<tr>
<td>State:&nbsp;</td>
<td>
<div id=”txtstate”>
<select name=”state” id=”state” style=”width: 146px;”>
<option value=”0″ selected=”selected”>&nbsp;</option>
</select>
</div>
</td>
</tr
[/code]

selectstate.js code:

[code]
var xmlhttpco;

function showState(str)
{
xmlhttpco=GetXmlHttpObject();
if (xmlhttpco==null)
{
alert (“Browser does not support HTTP Request”);
return;
}
var url=”getstate.php”;
url=url+”?q=”+str;
url=url+”&sid=”+Math.random();
xmlhttpco.onreadystatechange=stateChangedco;
xmlhttpco.open(“GET”,url,true);
xmlhttpco.send(null);
}

function stateChangedco()
{
if (xmlhttpco.readyState==4)
{
document.getElementById(“txtstate”).innerHTML=xmlhttpco.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject(“Microsoft.XMLHTTP”);
}
return null;
}
[/code]

getstate.php code:

[code]
<?php
session_start();
global $DB, $INFO;
require (‘conf/db.class.php’);

$DB = new dbCon;
$DB->link[‘dbname’] = $INFO[‘dbname’];
$DB->link[‘dbuser’] = $INFO[‘dbuser’];
$DB->link[‘dbpass’] = $INFO[‘dbpass’];
$DB->link[‘dbhost’] = $INFO[‘dbhost’];

$DB->dbConnect();

$q=$_GET[‘q’];

$sql=”SELECT * FROM `states` WHERE `countryid` = ‘”.$q.”‘ ORDER BY `sname`”;

$result = $DB->query($sql);?>

<select name=”state” id=”state” style=”width: 146px;” onchange=”showCity(this.value);showKeywords(this.value);showKeywords2(this.value);showKeywords3(this.value);showKeywords4(this.value);”>
<option value=”0″ selected=”selected”>&nbsp;</option>
<? while($row = mysql_fetch_array($result)) { ?>
<option value=”<?=$row[0]?>”><?=ucwords($row[1])?></option>
<? } ?>
</select>
[/code]

Any and all help will be much appreciated.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@J-v-NauthorOct 12.2009 — Oh and I forgot to mention that this [B]works perfectly[/B] in IE8 but [B]doesn't work[/B] in firefox and Safari.

Also the original page HTML seems to be cut off so here is the rest of it.

<i>
</i>
<i> </i> &lt;tr&gt;
<i> </i> &lt;td&gt;State:&amp;nbsp;&lt;/td&gt;
<i> </i> &lt;td&gt;
<i> </i> &lt;div id="txtstate"&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/td&gt;
<i> </i> &lt;/tr&gt;

<i> </i> &lt;tr&gt;&lt;td&gt;City Name:&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="cname" size="20" /&gt;&lt;/td&gt;&lt;/tr&gt;
<i> </i> &lt;tr&gt;&lt;td colspan="2" align="center"&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type="submit" name="AddCity" value="Add City" /&gt;&lt;/td&gt;&lt;/tr&gt;
<i> </i> &lt;/form&gt;
<i> </i> &lt;/table&gt;
<i> </i> &lt;/fieldset&gt;
<i> </i> &lt;? } // Close Window

<i> </i> else { ?&gt;
<i> </i> &lt;script language="JavaScript" type="text/javascript"&gt;
<i> </i> /*&lt;![CDATA[*/
<i> </i> window.location = "&lt;?=$INFO['siteaddr']?&gt;/Error/1"
<i> </i> /*]]&gt;*/
<i> </i> &lt;/script&gt;
<i> </i> &lt;? } ?&gt;
<i> </i> &lt;/td&gt;&lt;/tr&gt;
<i> </i>&lt;/table&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@J-v-NauthorOct 13.2009 — The solution is to take the <form> and </form> tags outside of the <table> </table> tags
×

Success!

Help @J-v-N spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...