/    Sign up×
Community /Pin to ProfileBookmark

PHP MVC Ajax issue

Hi all, wonder if anyone has a moment, trying to create a dynamic dropbox that is populated from the selection of an original dropbox using ajax ( i know this part is working because the secondary dropbox alters with no data as soon as the original “customerID” box is altered, this seems to be very problematic because my “bootstrapper” which inspects the URL for the MVC models/functions is picking up the ajax request and basically saying it doesnt exist, if anyone could point me in the rite direction that would be great.

the contactSelect.php page that the ajax calls –

[code=php]<?php

$customerID = $_POST[‘customerID’];
$conn2 = new mysqli(DB_HOST,DB_USER, DB_PASS, DB_NAME)
or die (‘Cannot connect to db’);
$result2 = $conn2->query(“SELECT * FROM contacts where CustomerID=”.$customerID);
while ($row = $result2->fetch_assoc()) {

?>
<option value=”<?php echo $row[‘ContactID’]; ?>”><?php echo $row[‘ContactName’]; ?></option>
<?php
}

?>
[/code]

the ajax request that index.tpl calls when the customerID dropbox selection is altered

[CODE] <script type=”text/javascript”>
$(document).ready(function(){
$(“#customerID”).change(function(){
var customerID = $(“#customerID”).val();
$.ajax({
type:”post”,
url:”contactSelect.php”,
data:”customerID=”+customerID,
success: function(data) {
$(“#contactDrop”).html(data);
}
});
});
});
</script>[/CODE]

The original selection drop down also in index.tpl

[code=php] <select name=”customerID” id=”customerID” >
<?php
$conn = new mysqli(DB_HOST,DB_USER, DB_PASS, DB_NAME)
or die (‘Cannot connect to db’);
$result = $conn->query(“SELECT * FROM customer”);

while ($row = $result->fetch_assoc()) {

echo ‘<option value=”‘.$row[‘customerID’].'”>’ . $row[‘customerName’] . ‘</option>’;
}

?>
</select>[/code]

The bootstrapper page which controlls the MVC flow

[code=php]<?php
$controller = “contact”;
$action = “index”;
$query = null;

if (isset($_GET[‘load’]))
{
$params = array();
$params = explode(“/”, $_GET[‘load’]);

$controller = ucwords($params[0]);

if (isset($params[1]) && !empty($params[1]))
{
$action = $params[1];
}

if (isset($params[2]) && !empty($params[2]))
{
$query = $params[2];
}
}

$modelName = $controller;
$controller .= ‘Controller’;
$load = new $controller($modelName, $action);

if(!empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’)

{ //some sort of direct call to the php page should be created here because its an ajax request }

else
{

if (method_exists($load, $action))
{
$load->{$action}($query);
}
else
{
//die(‘Invalid method. Please check the URL. Action is: ‘.$action.’ and query is ‘.$query);
$load->{$action}($query);
}}[/code]

IE9 dev tools states that: <br />
<b>Fatal error</b>: Call to undefined method ticketcontroller::contactSelect.php() in <b>C:inetpubmvcutilitiesbootstrap.php</b> on line <b>39</b><br />

so my understanding is that the MVC is trying to pickup the ajax request, i put it a condition which checks if its an ajax but I dont know how best to handle the action, sorry this is so long winded, thanks alot in advance.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@nevermind86authorJun 14.2013 — resolved all, basically rather than mess around trying to exclude ajax calls from the mvc logic just included the method in the appropriate controller as below

public function getContact()

{

$customerID = $_POST['customerID'];

$conn = new mysqli(DB_HOST,DB_USER, DB_PASS, DB_NAME)
or die ('Cannot connect to db');

$result = $conn->query("SELECT * FROM contact where CustomerID=".$customerID);
while ($row = $result->fetch_assoc()) {

?>
<option value="<?php echo $row['ContactID']; ?>"><?php echo $row['ContactName']; ?></option>

<?php

}

}[/QUOTE]
×

Success!

Help @nevermind86 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.15,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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