...

View Full Version : jQuery jQuery Change Function



CoolAsCarlito
02-04-2011, 04:04 PM
When the user gets to the control panel it uses a gets the session data and assigns a variable $defaultCharID, which works by the way, with the dropdown I need the selection, if there is one changed, change the value of the variable from the option value. Below is a little bit of code from a very similiar type script and my intent is to make it more suitable for my page with the rest of the coding. As you can see it was written in javascript but I'm trying to modify it so I can have less code and I'm using jQuery on my page. I do have a start but not sure what I would need after this.


print "<form method=POST name=changedefaultcharacter>\n";
print "<input type=hidden name=newdefaultcharacterid value=0>\n";
print "</form>\n";

print "<script type=\"text/javascript\" language=\"javascript\">\n";
print "function executeformchangedefaultcharacter(newdefaultcharacterid) {\n";
print "document.changedefaultcharacter.newdefaultcharacterid.value = newdefaultcharacterid;\n";
print "document.changedefaultcharacter.submit();\n";
print "}\n";
print "</script>\n";

print "<form method=POST name=changecharacter>\n";
print "<select name=newdefaultcharacterid class=dropdown>";




$('#newDefaultCharID').change(function() {
var newDefaultCharID = $("select#newDefaultCharID").val();

});

<form action="#" id="changeCharacter">
<select class="dropdown" name="newDefaultCharID" id="newDefaultCharID" title="Select Character">

CoolAsCarlito
02-09-2011, 05:08 AM
Any ideas?

venegal
02-09-2011, 01:19 PM
Your question is very hard to understand. What exactly is it that you want to do? What you're currently doing is define a local variable when the dropdown is changed, which is a bit useless, as the variable will be gone as soon as the change handler has finished.

CoolAsCarlito
02-09-2011, 04:27 PM
Let me rephrase it as I have difficulty describing issues and questions sometimes.

The top code in my original post was from a fellow programmer with a similar script. My intentions was to pretty much take his bit of code and translate it into a jQuery shortened code. The bottom section of code was my attempt at accomplishing this but I'm sure if I'm even close yet.

venegal
02-09-2011, 06:08 PM
The first snipped defines a function that would submit a form, but the function is never actually called in that snippet, so it's hard to say what you actually want.

Why don't we just forget that first snippet (which is badly done, too), and you tell me in words, what you want?

For example: "I have a form with a dropdown in it, and once the user selects something, I want the form to be automatically submitted" or whatever. Just plain english, not some dated, incomplete and hard to read snippet.

CoolAsCarlito
02-09-2011, 07:00 PM
Okay now if you notice I've posted all the necessary code that will help hopefully explain this better.



<?php

session_start(); // Access the existing session

// Include the database page
include ('inc/dbconfig.php');

// Include the variables page
include ('inc/variables.php');

$userID = $_SESSION['userID'];
$isAdmin = $_SESSION['isAdmin'];
$defaultCharID = $_SESSION['defaultCharID'];
$defaultCharacterName = $_SESSION['defaultCharacterName'];
$firstName = $_SESSION['firstName'];
$lastName = $_SESSION['lastName'];
$defaultCharShortName = $_SESSION['shortName'];

?>




<script>
$(document).ready(function() {
$('#newDefaultCharID').change(function() {
var newDefaultCharID = $('#newDefaultCharID option:selected').val();
$('#characterID').val(newDefaultCharID);
$.ajax({
type: "POST"

});
});
});
</script>




<div id="headerCharacter">
<?php
if ($isAdmin == "Yes") {
$query = "SELECT
characters.ID
FROM
characters";
$result = mysqli_query ($dbc,$query);
$totalNumCharacters = mysqli_num_rows($result);

$query = "SELECT
handlercharacters.ID
FROM
handlercharacters
INNER JOIN
handlers
ON handlers.ID = handlercharacters.handlerID";
} else {
$query = "SELECT
handlercharacters.id
FROM
handlercharacters
INNER JOIN
handlers
ON handlers.ID = handlercharacters.handlerID
WHERE
handlers.ID = '".$userID."'";
}
$result = mysqli_query ($dbc,$query);
$numAvailableCharacters = mysqli_num_rows($result);
if (($numAvailableCharacters > "1") || (($isAdmin == "Yes") && (isset($totalNumCharacters)) && ($totalNumCharacters > "0"))) {
?>
<input type="hidden" name="characterID" id="characterID" value="0" />
<select class="dropdown" name="newDefaultCharID" id="newDefaultCharID" title="Select Character">
<?php

if ($defaultCharID > "0") {
print "<option value=".$defaultCharID.">".$defaultCharacterName;
} else {
print "<option value=0>- Select -";
}
if ($isAdmin == "Yes") {
$query = "SELECT
characters.ID,
characters.characterName
FROM
characters
WHERE
characters.ID <> '".$defaultCharID."' AND
characters.statusID = '1'
ORDER BY
characters.characterName";
} else {
$query = "SELECT
characters.ID,
characters.characterName
FROM
characters
INNER JOIN
handlercharacters
ON characters.ID = handlercharacters.characterID
INNER JOIN
handlers
ON handlers.ID = handlercharacters.handlerID
WHERE
handlers.ID = '".$userID."' AND
handlercharacters.characterID <> '".$defaultCharID."' AND
characters.statusID = '1'
ORDER BY
characters.characterName";
}
$result = mysqli_query ($dbc,$query);
$numRows = mysqli_num_rows ($result);
if ($numRows > 0) {
if ($isAdmin == "Yes") {
print "<optgroup label=\"** Active Characters **\">";
}
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
print "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r";
}
}
if ($isAdmin == "Yes") {
$query = "SELECT
characters.ID,
characters.characterName
FROM
characters
WHERE
characters.ID <> '".$defaultCharID."' AND
characters.statusID = '2'
ORDER BY
characters.characterName";
} else {
$query = "SELECT
characters.ID,
characters.characterName
FROM
characters
LEFT JOIN
handlercharacters
ON characters.ID = handlercharacters.characterID
LEFT JOIN
handlers
ON handlers.ID = handlercharacters.handlerID
WHERE
handlers.ID = '".$userID."' AND
handlercharacters.characterID <> '".$defaultCharID."' AND
characters.statusID = '2'
ORDER BY
characters.characterName";
}
$result = mysqli_query ($dbc,$query);
$numRows = mysqli_num_rows($result);
if ($numRows > "0") {
print "<optgroup label=\"** Inactive Characters **\">";
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
print "<option value=\"".$row['ID']."\">".$row['characterName']."</option>\r";
}
}
?>
</select>
<?php
} else {
print "<h1>".$defaultCharacterName."</h1>\n";
}
?>
</div>


What I'm wanting to do is if you notice after the person logs in and is taken to this page that holds all three blocks of coding (control panel) It establishes some variables based off the login credentials the user gave.

The purpose of the defaultCharID and defaultCharacterName is just to establish which is the main character the user is controlling AT THE TIME after login. Now with the dropdown I'm not sure how to handle this but I'm thinking after the selection it will create a new session(?) with the new characterName and its ID as the new default ID and character of currently controlling. If all of that makes sense.

venegal
02-10-2011, 09:04 AM
Sounds fine. Why do you want to use AJAX for that, though? Can't you just normally submit the form, and let your PHP give you back a new page with the changed character data?

CoolAsCarlito
02-10-2011, 03:19 PM
Is just one dropdown. There is no submit button.

venegal
02-10-2011, 05:37 PM
There's no such thing as "just one dropdown". Every dropdown is part of a form. It doesn't need to have a submit button; you can do the submitting onchange with Javascript.

CoolAsCarlito
02-10-2011, 05:40 PM
Right. I know that jQuery comes with a change event however this is where I get stuck I'm not sure where to go to from here. If you notice in the second block of code I have a change event started but from that point I'm clueless.

venegal
02-11-2011, 03:50 PM
What is there to get stuck on? Just submit the thing!



$('#newDefaultCharID').change(function() {
$(this).parents('form').submit();
});


Of course, your form needs method="POST" (which I don't see in your code), if you want to access the submitted values using the $_POST array on the server side.

CoolAsCarlito
02-13-2011, 04:33 PM
I don't understand how I can submit it when there's no form tag or submit button and why would I need those. All it is one dropdown so all I need is to perform the tasks for the change event.

venegal
02-13-2011, 05:11 PM
Why would there be no form tag? In both snippets in your first post, there are form tags. You don't need a submit button in order to submit, but there definitely has to be a real form, with an actual URL you're submitting to.

As for "why would I need those": I thought that's pretty obvious; you want to submit a form, in order to get a new page from the server that reflects the choice the user made in the dropdown. So, you need a form. Sure, you can get the new information per AJAX and then change your site manually, so it reflects the dropdown choice, but why go through that hassle?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum