PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingPHP 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 December 5th, 2004, 11:00 PM
aaa123 aaa123 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 aaa123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
php and javascript....(help needed)

hi,

I am having trouble getting this to work....here is the code below....it suppose to be a event driven code....the table(userProfile) is already created.....when you click the first radio button it pushes 1 into the table or else it pushes 2 into the table. But, for some reason the (if...else) structure is failing.

Can you give me a pointer or modify my code so that I can understand it .............I know php is server side and javascript is client-side.

---------------------------------------------------------------

<?php
/* Connecting, selecting database */
$link = mysql_connect(localhost , team03 , team03) or die("Could not connect");
mysql_select_db(newsreader) or die("Could not select database");
?>


<form name = "form" method="POST">

<td><font class="smallblue">Select News Type:</td> // below is where the radio buttons are being created, the name have to be the same
<td align=center width=10%><font class=medblue><input type=radio class=schedule name="R1" value="International News"><br>International News</td>
<td align=center width=10%><font class=medblue><input type=radio class=schedule name="R1" value="Sports News"><br>Sports News</td>


<script language = "javascript">
<!--
function save()
{
if (document.form.R1[0].checked) //test to see if the first radio button is clicked
{
alert("You have selected to receive International News on your PDA");


<?php
mysql_query("DELETE FROM userProfile"); // I am doing this b/c I want to remove the row, so that it will always be 1 row and not grow
$query = "INSERT INTO userProfile (choice) VALUES ('1')";
$result = mysql_query($query, $link);
?>
}
else
{
alert("You have selected to receive Sports News on your PDA");


<?php
mysql_query("DELETE FROM userProfile"); // I am doing this b/c I want to remove the row, so that it will always be 1 row and not grow
$query = "INSERT INTO userProfile (choice) VALUES ('2')";
$result = mysql_query($query, $link);
?>
}
<!--end script-->
</script>


<td class="mainftr" colspan="0" align="center">
<input type="hidden" name="pcount" value="1">
Click to save your setting:
<input class="submit" type="submit" name="process_reminder" value="Save Preferences" onclick = "save()"> //javascript is call here when the Save button is clicked
</form>
</tr></table>

<?php
/* Closing connection */
mysql_close($link);
?>

______________________________________

Ok...I am going to bed now.

Thanks
aaa123

Reply With Quote
  #2  
Old December 6th, 2004, 07:46 AM
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,886 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 19 m 35 sec
Reputation Power: 18
You need to remember that PHP and Javascript are two seperate languages. They don't quite work the way you seem to be trying.

PHP is a server side language, which means it is interpretted on the server before ever being sent to the browser.

Javascript, however, is executed on the browser and has no interaction at all with the server.

The best way to interact with your server [in terms of adding/deleting records] is using a form. Now, I see you have a form. In your example, however, I don't see the need for Javascript at all. Also, you should put quotes around the host, user, and password of your PHP/MySQL connect command.

Take a look at this:
PHP Code:
<?php
if(isset($_POST['R1']) {
    
/* Connecting, selecting database */
    
$link mysql_connect('localhost' 'team03''team03') or die("Could not connect");
    
mysql_select_db('newsreader') or die("Could not select database");

    
mysql_query("DELETE FROM userProfile"); // I am doing this b/c I want to remove the row, so that it will always be 1 row and not grow
    
$query "INSERT INTO userProfile (choice) VALUES ('".$_POST['R1']."')";
    
$result mysql_query($query$link);
    
mysql_close($link);
}
?>

<form name = "form" method="POST">

<td><font class="smallblue">Select News Type:</td>
<td align=center width="10%"><font class="medblue"><input type="radio" class="schedule" name="R1" value="1">International News</td>
<td align=center width="10%"><font class="medblue"><input type="radio" class="schedule" name="R1" value="2">Sports News</td>

<td class="mainftr" colspan="0" align="center">
<input type="hidden" name="pcount" value="1">
Click to save your setting:
<input class="submit" type="submit" name="process_reminder" value="Save Preferences">
</form>
</tr></table>


Now here's what I've done, aside from add quotes around numerous HTML attributes

I took out your Javascript altogether, I don't see the purpose aside from displaying the alert... I think adding Javascript to the example will only confuse you. You don't need javascript to use PHP.

I changed the value of your Radio boxes to represent the values in the database. This way the Insert statements look much simpler.

I do'nt understand why yo'ure deleting everything from the table before you insert, this is likely bad design. You may want to figure out a better way to do that. If you're using users, and you have a User table, you may need a userid field in that userpreferences table... just a thought...

I hope this puts you on the right track... I hope I didn't give you your code directly, as its better to discover thigns on your own... but again, I don't want to leave you astray...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > php and javascript....(help needed)


Developer Shed Advertisers and Affiliates


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap