942,520 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1096
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 30th, 2010
0

need help with php + javascript.

Expand Post »
hi everybody,

i encounter a problem regarding PHP when i am doing my internship.

i have a javascript which looks like this.


<Script Language = "JavaScript">
function sent()
{
var text1, fTxt,dT;
text1 = document.form1.EPTextField.value;
dT = new Date();
fTxt =("You entered" + ": " + text1 + " " + ":" + dT);
document.form1.EPText.value = fTxt;
}
</script>

where it will be called by onClick of a button

<input type="button" name="EPButton" id="EPButton" value="Send" onClick = "sent()"/>

However i need to save the fTxt into a database MySQL. my in charge called me to use php.

$con = mysql_connect("localhost","host"); //connect database
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("chat",$con); //select database
mysql_query("INSERT INTO chat (Name, Date, Content, Time)
VALUES ('fTxt')"); //insert

mysql_close($con);

?>

BUT here is the problem, my supervisor says that php doesn't understand what is the javascript value. so i need a platform like AJAX; i saw the ajax tutorial, i totally can't get it into my mind. so is there another way i can get it to work? or perhaps i can change the javascript function into a php function. but i tried googling "how to declare a var as a textbox value in php" and didnt get what VAULT said. I hope i can get some help here.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hongchai is offline Offline
8 posts
since Jun 2010
Jun 30th, 2010
0
Re: need help with php + javascript.
It's either AJAX or you need to submit a form with the value as a hidden input.

~G
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
400 posts
since Aug 2009
Jun 30th, 2010
0
Re: need help with php + javascript.
i am doing a LAN MESSENGER which look like a facebook taskbar. GUI done... send button done... now it's how to throw the result tat is generated by javascript and store it inside MYSQL database using PHP.... any1 can guide me?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hongchai is offline Offline
8 posts
since Jun 2010
Jun 30th, 2010
0
Re: need help with php + javascript.
You can write the php code as a seperate file and use the $_GET method to get the value from the url

<Script Language = "JavaScript">
function sent()
{
var text1, fTxt,dT;
text1 = document.form1.EPTextField.value;
dT = new Date();
fTxt =("You entered" + ": " + text1 + " " + ":" + dT);
document.form1.EPText.value = fTxt;
window.location="update.php?value="+fTxt;
}
</script>

It will open update.php page with the follwing url

http://localhost/update.php?value=test;

where test is the value of ftxt. you can utilize the value by $value=$_GET['value'];
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Jun 30th, 2010
0
Re: need help with php + javascript.
thanks raja for ur reply. so i should create a new file call update.php??? what shld i put inside?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hongchai is offline Offline
8 posts
since Jun 2010
Jun 30th, 2010
0
Re: need help with php + javascript.
PHP Syntax (Toggle Plain Text)
  1. $value=$_GET['value'];
  2. echo $value; //Ensure that you received the value by print on the page
  3. $con = mysql_connect("localhost","host"); //connect database
  4. if (!$con)
  5. {
  6. die('Could not connect: ' . mysql_error());
  7. }
  8. mysql_select_db("chat",$con); //select database
  9. mysql_query("INSERT INTO chat (Name) VALUES ('$value')");
  10. mysql_close($con);
  11. ?>
Last edited by rajarajan07; Jun 30th, 2010 at 3:58 am.
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Jun 30th, 2010
0
Re: need help with php + javascript.
as i you see your trying to implement a chatting system. Use form submission, instead of onclick events

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. if($_POST['btnsubmit']){
  4.  
  5. $user = $_POST'user'];
  6. $msg = $_POST['msg'];
  7.  
  8. $con = mysql_connect("localhost","host"); // i hope you can already connect successfully
  9. if (!$con)
  10. {
  11. die('Could not connect: ' . mysql_error());
  12. }
  13.  
  14. $now = time(); // now
  15.  
  16. $date = date("Y-m-d",$now);
  17. $time = date("h:i:s",$now);
  18.  
  19. mysql_select_db("chat",$con);
  20. mysql_query("INSERT INTO chat (Name, Date, Content, Time) VALUES ('".$user."','".$date."','".$msg."','".$time."')"); // i hope you set your date field to data type date, and you set your time field to data type time
  21.  
  22. mysql_close($con);
  23.  
  24. }
  25.  
  26. ?>
  27. <form method="post">
  28. User: <input type="text" name="user" /><br />
  29. Message: <textarea name="msg"></textarea><br />
  30. <input type="submit" name="btnsubmit" value="send" />
  31. </form>


but if the scenario really require onclick events then, ill re-design the code, the reason why i abandon your javascript coz im kinda puzzled on what you really tryin to save, like this part

PHP Syntax (Toggle Plain Text)
  1. fTxt =("You entered" + ": " + text1 + " " + ":" + dT);

i assume you storing ftxt to string, something like "You entered: hello!:Wed Jun 30 2010 17:18:23 GMT+0800 (Taipei Standard Time)" the Date() in javascript returns a formatted date, unlike PHP date which returns timestamp. and your insert is incorrect

PHP Syntax (Toggle Plain Text)
  1. mysql_query("INSERT INTO chat (Name, Date, Content, Time)
  2. VALUES ('fTxt')"); //insert

your inserting ftxt to where? it should be

PHP Syntax (Toggle Plain Text)
  1. mysql_query("INSERT INTO chat (Name, Date, Content, Time)
  2. VALUES ('your_name','the_date','your_content','the_time')")// just a sample
Last edited by vaultdweller123; Jun 30th, 2010 at 6:46 am.
Reputation Points: 32
Solved Threads: 55
Posting Pro in Training
vaultdweller123 is offline Offline
448 posts
since Sep 2009
Jun 30th, 2010
0
Re: need help with php + javascript.
Thanks raja and vault for ur kind reply : ) i can feel some warm from this forum : )

Example user enters "Hello! how are u guys doing. : Wed Jun 30 2010 17:18:23 GMT+0800" into the textarea1.. it will appear in textarea2. The text that the user entered, must be save into a database. but i encounter some problem when i am thinking about it. my table 'chat' has 4 table / attributes(Name,Content,Date,Time), how am i going to split a full text string, "You entered: Hello! how are u guys doing. : Wed Jun 30 2010 17:18:23 GMT+0800" into 4 different short string like "You entered: " , "Hello! how are u guys doing." , "Wed Jun 30 2010" , "17:18:23 GMT+0800" and store inside my tables?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hongchai is offline Offline
8 posts
since Jun 2010
Jun 30th, 2010
0
Re: need help with php + javascript.
try running my code so you can understand
Reputation Points: 32
Solved Threads: 55
Posting Pro in Training
vaultdweller123 is offline Offline
448 posts
since Sep 2009
Jul 1st, 2010
0
Re: need help with php + javascript.
thanks vault. i got some questions for you. a little background: my lan messenger looks like facebook messenger, when u click a small tab. it pops up a division; into the division there is a textarea2(displaying message in textarea1) and textarea1(message).
1. i tried using submit, however after i click submit, the division disappear. therefore i change to a 'button'. so shld i stay on my 'button' / 'submit'?
2. .$user. << why does the php var has 2 dots. what does it means?
3. should i redo my javascript function into PHP function?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hongchai is offline Offline
8 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Help with my php calendar
Next Thread in PHP Forum Timeline: Dynamic meta tags on large websites.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC