Calling a PHP function from Javascript, Writing HTML in Javascript
Author |
Message |
samara
|
Posted: Thu Jun 04, 2009 1:04 pm Post subject: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
Approach 1:
<html>
<body>
<form name = "form1">
<textarea name = "txtarea1" id = "txtarea1" rows = "3" cols = "30" ></textarea><br>
<input type = "button" value ="Submit Comment" onclick = "addItem()"/>
</form>
<script type = "text/javascript">
var a;
function addItem() {
a = document.getElementById("txtarea1").value;
document.write('<td><script type="text/javascript">document.write(a);</script></td>'); ----------------------------ERROR
}
</script>
<table class="irtable" border="1">
<tr>
<th>Comment</th> </tr>
<td><script type="text/javascript">document.write(a);</script></td>
<td><input type = "button" Value ="Delete"/></td>
</table>
</body>
</html>
The code at the place error is not showing up like a javascript, and it is not executing!!
Can anyone help me, any suggestions please!! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
octopi
|
Posted: Thu Jun 04, 2009 1:09 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
Perhaps if you can better explain your end target/goal we can help you out. |
|
|
|
|
|
samara
|
Posted: Thu Jun 04, 2009 1:16 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
Approach 2:
Comments.Php Follows:
<html>
<body>
<input type = "text" name "comment" style="width: 99%;" id = "comment"/><br>
<input type = "button" value ="Add" onclick = "addcomment()"/>
<script type = "text/javascript">
var a,b,d;
var count = 0;
function addcomment() {
a = document.getElementById("Comment").value;
if (count == 0) {
d = a; // d is the original value of the textarea before an additions
count ++;
}
'<?php $report="<script>document.write(a)</script>"; ?>';
document.getElementById("comment").value = "";
self.location="Action.php?action=1";
}
</script>
</body>
</html>
Action.Php Follows:
<html>
<body>
<?php
if(isset($action) && $action == 1){
echo_header_detail();
}
function echo_detail_header()
{
?>
<table class="irtable" border="3">
<tr>
<th>Comments</th>
<td Width="110"><script type="text/javascript">document.write(a);</script></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
When executing Comments.php and clicking on Add button, its getting transfered to Action.php but the code echo_header_detail is not getting executed.
Can anyone help me!! If it gets executed i also want to pass the value a from comments.php to action.php
Any help r suggestions please!! |
|
|
|
|
|
samara
|
Posted: Thu Jun 04, 2009 1:24 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
Octopi, Thanks for the quick turn over sir!!
I have to implement a comments section under a webpage which displays a report!!
1.) User should be able to add comments.
2.) Unfortunately we donot have any database!!
So when ever user adds comment and click add, the value in the textarea or textfield wherever he enters should be stored on the page as HTML content and an update and delete button should be generated beside the comment!!
My boss says, since there is no database, and users are less, whenever someone adds comments, the whole page should refresh r whatever you have to edit the html content of the whole page.
so i wan to carete a textfiled and button, when user enters and clicks button, function should be called and it should get value from the textfield and display it somewhere on the same page!!
i am thinking of displaying it as a table Like:
-----------
[Text Field] [add Button]
------------
when add is clicked, the page changes to
-----------
[Text Field] [add Button]
[Table Header]
[comment in row] [update button] [delete button]
----------- |
|
|
|
|
|
octopi
|
|
|
|
|
samara
|
Posted: Thu Jun 04, 2009 1:47 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
Oh My God!!
this is the best reply i saw from so many posts!!
today is my second day in PHP, the links you gave, i was not able to understand how to implement it!!
can you please write a piece of code for my situation, assuming any text field or button. |
|
|
|
|
|
octopi
|
Posted: Thu Jun 04, 2009 1:53 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
code: |
<?php
if($_REQUEST['comment']) { file_put_contents("comment.txt", $_REQUEST['comment'],FILE_APPEND); }
print "<html>";
print "<form method='POST'>";
print "Comment: <input type='text' name='comment' /><input type='submit' name='submit' value='Post' />";
print "</form>";
print @file_get_contents("comment.txt");
print "</html>";
?>
|
|
|
|
|
|
|
samara
|
Posted: Thu Jun 04, 2009 2:07 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
I am giving a little explanation for the code you gave me:
Comment.txt is the file name where comments are stored!!
Comment is the text field,
Submit is the button which displays post on it,
Now is $_REQUEST[?comment?] is the text in the comment and if($_REQUEST[?comment?] means if button is clicked?
So this whole thing is only, when user enters comment and clicks the button, the values are written into the file and the file is refreshed or shown on the same page, am I correct!! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
octopi
|
Posted: Thu Jun 04, 2009 2:10 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
comment.txt is the file where the comments are stored, correct.
comment is the text field
the submit just saves the comment post
$_REQUEST['comment'] gets the value of the text field
if($_REQUEST['comment']) {} means the text field has something inside of it.
It writes the new comment to the file, and then reads the contents of the file and displays it. |
|
|
|
|
|
samara
|
Posted: Thu Jun 04, 2009 2:26 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
I implemented it, i am getting two an error:
Undefined index: comment in C:\Program Files\NComments.php on line 2
and line 2: is this one "if($_REQUEST[?comment?] "
one more thing: i am getting all the data displayed in a row, i want them line after line,
Now if user wants to delete a specific comment what should he do!! |
|
|
|
|
|
octopi
|
Posted: Thu Jun 04, 2009 2:50 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
That isn't an error, its a notice.
You could disable notices, or use the array_key_exists() method to check if the index exists before attempting to access it.
http://ca3.php.net/array_key_exists
Add a <br /> tag to what you write to the file.
code: | if($_REQUEST['comment']) { file_put_contents("comment.txt", $_REQUEST['comment'] . "<br />",FILE_APPEND); } |
There is no way to delete a specific comment this way, you should really be using a database. Alternatively you can create a different comments file each time, and then read them all back in....but this is way beyond your current abilities. Perhaps you should read up on some tutorials first, before jumping head first into a job. |
|
|
|
|
|
samara
|
Posted: Thu Jun 04, 2009 3:15 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
Thanks a lot sir,
I do not have more than 25 posts to increase your Karma or give you points or something!! I still have so many doubts and since i am new, i will be getting doubts,
will it be ok, if i ask doubts everyday or often!!
Thanks a lot once again!!
As of now, i have one doubt:
Since the deletion of specific comment is too complex at this time and moreover, since the comments are getting displaying one after another in a row
Is there anyway, i can put a date and time, or user name beside each comment
ie beside each $_REQUEST['comment'] when ever the files appends the text, may be a way append present date and time beside it!! |
|
|
|
|
|
octopi
|
Posted: Thu Jun 04, 2009 3:19 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
If you want you can add another field, such as username
then do
code: | if($_REQUEST['comment']) { file_put_contents("comment.txt", $_REQUEST['username'] . " wrote: " $_REQUEST['comment'] . "<br />",FILE_APPEND); } |
remember to add the text field to the html form section
if you want the date and time then do something like this
code: |
$date = date("F j, Y, g:i a");
if($_REQUEST['comment']) { file_put_contents("comment.txt", $_REQUEST['username'] . " wrote: " $_REQUEST['comment'] . " on " . $date . "<br />",FILE_APPEND); }
|
http://ca3.php.net/manual/en/function.date.php |
|
|
|
|
|
samara
|
Posted: Thu Jun 04, 2009 4:28 pm Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
Hello Sir,
I have new problem, my boss walked in just now!!
When ever user comment and clicks add, i need to open an HTML file from a directory(i need to open an existing file which i know), i need to locate a string </head> and i need to put the value of the text field there like this:
<meta name="comment" content="value in the text field ie ($_REQUEST['comment']">
now i have this
if($_REQUEST['comment'])
{
$file_contents = file_get_contents(REPORTS_DIR."/$FILENAME");
$find_string = '</head>';
now how to add the new string <meta name="comment" content="value in the text field ie ($_REQUEST['comment']"> above the </head>
Pls lemme knw!!
Thanks!! |
|
|
|
|
|
samara
|
Posted: Fri Jun 05, 2009 8:52 am Post subject: Re: Calling a PHP function from Javascript, Writing HTML in Javascript |
|
|
My Next step is:
Now user has enetered the comment and he posted, its going into the file:
Now the files meta Tag is like this:
<meta name="comment" content="This is the first">
If user enters a new comment(this is the second comment), it should append to the first comment in a new line like this:
<meta name="comment" content="This is the first
This is the second comment">
Any help please
Quote: function addMetaTag($file, $separator, $metaTag)
{
$file_contents = file_get_contents($file);
$file_parts = explode($separator, $file_contents);
$newFile = $file_parts[0].$metaTag.$separator.$file_parts[1];
file_put_contents($file, $newFile, LOCK_EX);
}
if(isset($_REQUEST['comment']) && $_REQUEST['comment'])
{
$file = REPORTS_DIR."/$FILENAME";
$separator = "</head>";
$metaTag = "<meta name=\"comment\" content=\"".$_REQUEST['comment']."\">\r\n";
addMetaTag($file, $separator, $metaTag);
}
// See the results. //
//echo "<span style='font-size:24px;font-weight:bold;'>New file :</span><br />".nl2br(htmlspecialchars(file_get_contents($file)));
print "<html>";
print "<form method='POST'>";
print "<div class='normal_header'>Comment:</div><textarea name = 'comment' id = 'txtarea1' rows = '3' cols = '30' ></textarea/></br><input type='submit' name='submit' value='Post' />";
print "</form>";
print "</html>"; |
|
|
|
|
|
|
|