![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 54
Rep Power: 4
![]() |
PHP + Javascript(?)
Hi all,
I am relatively new in PHP and I have come into a problem, that, I think, is solved by using Javascript The problem is the following: I have a form where the user inserts a query, e.g :"Retrieve all tha names of the students". The next page, which is written in PHP gets the result, e.g: "Nick", "John", "Mary". What I want to do is to have something like a small box near each name, which the user can tick and then see the student's page for examlpe. It is sort of a 'select button', and the user can select one or more students to view their pages. Q1: Is this done with Javascript only or can I use PHP as well? Q2: Because I don't know anything about Javascript, is there any short code that can do my job easily? Thanx in advance |
|
|
|
|
|
#2 |
|
Programmer
|
Well, I think you mean like a box with a checkmark in it. I don't know PHP, but I do know that you can have a checkbox with simply HTML. You can control what it does with PHP I'm guessing, but to make it, all you need to know is HTML.
<form> <input type="checkbox" name="Nick"> Nick <br> <input type="checkbox" name="John"> John <br> <input type="checkbox" name="Mary"> Mary <br> </form> This code will put 3 checkboxes with the words Nick, John and Mary beside them. Again, I don't know PHP, so you might want to wait for someone that does, but I think that you can control what happens when these checkboxes are selected with PHP. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It's hard to tell precisely what you realize. When you look at a page with PHP and HTML and Javascript intermixed, you are not looking at interactivity; you are looking at plans for constructing a page which will ultimately be sent to the client. The connection will then be dropped. It is a stateless protocol working in a request/response paradigm.
Any information you want to manipulate with Javascript (client-side JS, anyway) has to reside on the client. It may be present in a .js file or explicit inside of script tags or generated on the client. If you want information in the page that reflects the contents of PHP variables, then you have to put it there by one mechanism or another. Quite often a new request is sent to the server which loads a new page, or reloads the existing page, with the expanded information. Sometimes the full set of information is conveyed in the original page, but hidden until some client-side action is taken. There are also ways of making ancilliary requests without reloading the entire page.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Programmer
|
You'll have to modify this, since this is for an online game we created for class.
But using checkboxes, this is a start on how you can grab just the ones checked and place them on a new page. <script language="javascript" type="text/javascript">
// global variable to track the number of checkboxes that are checked
var checkCount = 0
// increment or decrement the checkbox counter
function validate(checkbox)
{
if (checkbox.checked)
{
checkCount++
}
else
{
checkCount--
}
}
// check that only 1 to 5 characters are checked before posting the form
function submitIt()
{
if (checkCount > 5 || checkCount < 1)
{
alert ("Limit of 5 characters per party.")
}
return (checkCount <= 5 && checkCount > 0)
}
</script>Keep in mind you actually have to have your html form call this function. I can't really give you much more than that, but perhaps there is someone that can help you figure it out with this tidbit of information. If it helps you at all, you can actually generate PHP script using Javascript for dynamic updates. Not too sure how you would do that with PHP, or if you can, I am fairly fimilar with PHP, but by no means a guru. lol
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former. |
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#6 | |
|
Programmer
|
Quote:
<script type="text/javascript" language="javascript">
strength = new Array;
vitality = new Array;
dexterity = new Array;
agility = new Array;
intelligence = new Array;
wisdom = new Array;
<?php
$sql = "SELECT ID, strength, vitality, dexterity, agility, intelligence, wisdom FROM Class";
$result = @mysql_query($sql,$db)or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "strength[" . $row['ID'] . "] = " . $row['strength'] . ";\n";
echo "vitality[" . $row['ID'] . "] = " . $row['vitality'] . ";\n";
echo "dexterity[" . $row['ID'] . "] = " . $row['dexterity'] . ";\n";
echo "agility[" . $row['ID'] . "] = " . $row['agility'] . ";\n";
echo "intelligence[" . $row['ID'] . "] = " . $row['intelligence'] . ";\n";
echo "wisdom[" . $row['ID'] . "] = " . $row['wisdom'] . ";\n";
}
?>
function updateStats()
{
document.getElementById("strength").innerHTML = strength[document.charCreate.charClass.selectedIndex + 1];
document.getElementById("vitality").innerHTML = vitality[document.charCreate.charClass.selectedIndex + 1];
document.getElementById("dexterity").innerHTML = dexterity[document.charCreate.charClass.selectedIndex + 1];
document.getElementById("agility").innerHTML = agility[document.charCreate.charClass.selectedIndex + 1];
document.getElementById("intelligence").innerHTML = intelligence[document.charCreate.charClass.selectedIndex + 1];
document.getElementById("wisdom").innerHTML = wisdom[document.charCreate.charClass.selectedIndex + 1];
}
</script>I have to correct that, meaning you can place PHP code within a javascript tag, lol.
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former. |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
That would be generating Javascript using php
You CAN generate php using javascript if you really wanted, but not on the same page, you'd have to write the php to another file then request that file
__________________
#programmingforums relay - http://thegupstudio.com/cgi-bin/pforelay.cgi freelance scripts - http://ryanguthrie.com/index.html |
|
|
|
|
|
#8 |
|
Programmer
|
eh?
pfft. LOL Yea, I am backwards today. Not much sleep. Although that last bit is good to know.
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former. |
|
|
|
|
|
#9 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|