JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 9th, 2004, 11:15 AM
piter piter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 piter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How does javascript direct to PHP?

Hii all,

currently, I have a problem to direct my javascript to PHP after checking for data validation.
I want to connect to MySQL by using the PHP by using submit() in javascript. I use onclick() in
the button in HTML form, and use document.myform.submit() in javascript to acess the database connect,
but it cannot.

The code as following:

<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function checkform() {
team1 = document.gameform.team1tf.value;
if (team1 == "") {
alert("Enter the first team of Game ");
document.gameform.team1tf.focus();
return; }

document.gameform.submit();
}
</head>
<body>
<?
if(isset($submit)){
$db= mysql_connect("localhost","root","");
$selectdb = mysql_select_db("one2soccer1");
$query = "select tipserial1 from game where gamename= \"\"order by tipserial1";
$result = mysql_query($query); }
?>
.
.
<form name="gameform" method="post" action="<?PHP_SELF?>">
<input type="button" name="submit" value="Submit" onClick="checkform()">
<input type="reset" name="reset" value="Reset">
</form>
</body>

Thank you for your information.

Regards,
Chupiter

Reply With Quote
  #2  
Old November 9th, 2004, 06:01 PM
pocketsized pocketsized is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Brunei
Posts: 26 pocketsized User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I think the problem is here. Change this:
PHP Code:
if(isset($submit)) 


to this:
PHP Code:
if (isset ($_POST ["submit"])) 


Try if that works.

Reply With Quote
  #3  
Old November 9th, 2004, 06:37 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
As an added note, I'd like to highly discourage the use of this line: $db= mysql_connect("localhost","root","");

NEVER use root unless for serious administation purposes... 90% of the time, you should be using a different, and limited account...
ALWAYS have passwords set for every account
AVOID posting the connectivity information in a public forum [heck, even a private forum]...

I'd recommend you create a user and grant them fairly limited permissions... if your web program simply reads from a database, only give them permissions to read that specific database... if you need to insert information into a table, give permissions to that specific table...

it never hurts to be too cautious of security threats, and always prevent them in as many locations as possible...

Reply With Quote
  #4  
Old November 9th, 2004, 10:14 PM
piter piter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 piter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by pocketsized
I think the problem is here. Change this:
PHP Code:
if(isset($submit)) 


to this:
PHP Code:
if (isset ($_POST ["submit"])) 


Try if that works.

------------------------------------------------------------
Hi pocketsize,

Thanks for ur solution, but your suggestion doesn;t work in the
script, there is an error occured.

Regards,
Piter

Reply With Quote
  #5  
Old November 9th, 2004, 10:16 PM
piter piter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 piter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi madcowzz,

Thanks for you guideance, I will work on that

Regards,
piter

Reply With Quote
  #6  
Old November 10th, 2004, 12:22 AM
pocketsized pocketsized is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Brunei
Posts: 26 pocketsized User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Strangely enough, I couldn't get the JS submit () function to work eventhough I'm sure I've used it before. Anyway, I changed some of the code. Your form should look like this:
Code:
 	<form name="gameform" method="post" action="<?PHP_SELF?>" onsubmit="return checkform()">
 
 	  <input type="submit" name="submit" value="Submit">
 	  <input type="reset" name="reset" value="Reset">
 	</form>
 

Note the changes: 1) The submit buttons is now a real submit button (type="submit") 2) The submit button no longer has the onclick eventhandler 3) The form has an onsubmit eventhanlder.

The JS should look like this:
Code:
 function checkform() {
 team1 = document.gameform.team1tf.value;
 if (team1 == "") {
 alert("Enter the first team of Game ");
 document.gameform.team1tf.focus();
 return false; }
 
 return true;
 }
 


I've tested it an it works on my end.

Reply With Quote
  #7  
Old November 10th, 2004, 01:16 AM
piter piter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 piter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hii pocketsized,

I ve tried to follow 3 changes ur guide.
If I used <?PHP_SELF?>, after press the submit button and correct data validation in javascript, then the browser cannot find the page. Therefore I refer to the page itself in the action="gameidentify.php". However, I try to print alert("") in after the submission, but there is nothing happened.

<?
//used this command to direct the javascript to PHP database,right?
if(isset ($_POST("Submit"))){
print("<script>alert(\"testing completed\")</script>");
}
?>

after I set return true, if the data validation in java script is correct.

Regards
Piter

Reply With Quote
  #8  
Old November 10th, 2004, 06:06 AM
pocketsized pocketsized is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Brunei
Posts: 26 pocketsized User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<?PHP_SELF?> is actually from your original code. Come to think of it, it should look like this <? $_SERVER ["PHP_SELF"] ?>

Anyway, the problem is that php is case-sensitive. You're checking for $_POST ["Submit"] with a capital S when the HTML doesn't have a form element with that name. Change it to $_POST ["submit"] with a lower-case s.

Code:
 <input type="submit" name="submit" value="Submit">
 


Notice that the name attribute is spelled "submit" with a lower-case s.

Reply With Quote
  #9  
Old November 10th, 2004, 06:36 AM
piter piter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 piter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
yap.....it works pocketsized........u re my man......but there is slightly a problem I encounter....

in my testing script: I put

if(isset ($_POST("submit"))){
print("<script>alert(\"testing completed\")</script>");
}

After the data validation has completed, then it will promt the alert message in javascript. However, if I press refresh button without input any value in the forms(textfield,etc), the program will prompt again the alert message without go to my javascript for data validation because there is a value in $_POST("submit").

So is there any way to clear all the value in $variable after the first process is completed.

Thank you.

Regards,
piter

Reply With Quote
  #10  
Old November 10th, 2004, 06:01 PM
pocketsized pocketsized is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Brunei
Posts: 26 pocketsized User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by piter
So is there any way to clear all the value in $variable after the first process is completed.

Well, after form submission, you could re-direct the user to another page where you display the JS alert.

Reply With Quote
  #11  
Old November 10th, 2004, 11:58 PM
piter piter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 piter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
oke I get it, thanks

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > How does javascript direct to PHP?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT