forms and php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomdagom
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 11:32 am

forms and php

Post by tomdagom »

Everah | Please use the bbCode tag [code] when posting code in the forums. For posting PHP code, use [code=php].

i am using freerte editor for my website ,i got this from the following url http://www.reerichtexteditor.com/
i am trying to pass that text the user enters in the rte to my mysql db, but i am having problems. this is my code

Code: Select all

<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
 
$conn = @mysql_connect("localhost", "*****", "*****");
 
 
if (!@mysql_select_db("info", $conn))
  die("<p>Error selecting DB-DEVEL database: <i>" . mysql_error() . "</i></p>");
  ?>
  <?php
  function send_data(){
   $date=date("Y")."-".date("m")."-".date("d");
$time=date("G").":".date("i").":".date("s");
 $max_query = "SELECT MAX(number) FROM blog";
  $max_result = mysql_query($max_query);
  while ($property = mysql_fetch_array($max_result))
        {
         $number = ($property["MAX(number)"]);
        }
        $number=$number+1;
$user="thomas";
$comments="0";
$year=substr(date("Y"),2,2);
$month=date("m");
$day=date("d");
$temp=$_GET['blog_content'];
echo("javascript&#058;alert('$temp');");
// mysql_query("INSERT INTO blog (blog_date,blog_content,poster,time,comments,number,month,day, year) VALUES ('$date','$blog_content','$user','$time','$comments','$number','$month','$day','$year')");
//mysql_query("INSERT INTO blog (blog_date, blog_content, poster, time, comments, number, month, day, year) VALUES  //('$date','$blog_content','$user','$time','$comments','$number','$month','$day','$year')");
    }
  ?>
<!-- Include the Free Rich Text Editor Runtime -->
<script src="rte/js/richtext.js" type="text/javascript" language="javascript"></script>
<!-- Include the Free Rich Text Editor Variables Page -->
<script src="rte/js/config.js" type="text/javascript" language="javascript"></script>
<!-- Initialise the editor -->
 
<script>
initRTE('this is just some preloaded content, this is just a test tp see where the string will fit and is it fits correctly');
</script>
   <form action="" method="get">
<INPUT type="hidden" NAME="blog_content" VALUE="getXHTML(trim(document.getElementById(rteFormName).value));">
 <input type="submit" name="submit" value="Submit!" onclick="<?php send_data();?>">
   </form>
   
<?php
 mysql_close ($conn);
?>
</body>
</html>

i am using echo("javascript:alert('$temp');"); just to check that the text is retrieved when i click submit. Any help would be appreciated.

Everah | Please use the bbCode tag [code] when posting code in the forums. For posting PHP code, use [code=php].
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: forms and php

Post by jimthunderbird »

I'm confused on this code:

Code: Select all

 
<input type="submit" name="submit" value="Submit!" onclick="<?php send_data();?>">
 
Javascript can not call a php function like that I think :?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: forms and php

Post by Zoxive »

It looks like you are trying to combine javascript and php. Almost like Ajax, but so far its completely wrong.

Code: Select all

onclick="<?php send_data();?>"
All that is doing is php is calling send_data(); Function when the php parser gets there. Not when the onclick is called(Javascript).

PHP is a server side language.

With the complete mess you have, im just going to show you the modified working way to do what you want.

Code: Select all

<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
 
// IF there is a $_POST['Submit'] do the function
if(!empty($_POST['submit'])) send_data();
 
$conn = @mysql_connect("localhost", "admin", "user1");
 
if (!@mysql_select_db("info", $conn))
die("<p>Error selecting DB-DEVEL database: <i>" . mysql_error() . "</i></p>");
?>
<?php
function send_data(){
$date=date("Y")."-".date("m")."-".date("d");
$time=date("G").":".date("i").":".date("s");
$max_query = "SELECT MAX(number) FROM blog";
$max_result = mysql_query($max_query);
while ($property = mysql_fetch_array($max_result))
{
$number = ($property["MAX(number)"]);
}
$number=$number+1;
$user="thomas";
$comments="0";
$year=substr(date("Y"),2,2);
$month=date("m");
$day=date("d");
$temp=$_POST['blog_content']; // Change this to Whatever the Textareas name is
echo '<br>',$temp,'<br>';
// mysql_query("INSERT INTO blog (blog_date,blog_content,poster,time,comments,number,month,day, year) VALUES ('$date','$blog_content','$user','$time','$comments','$number','$month','$day','$year')");
//mysql_query("INSERT INTO blog (blog_date, blog_content, poster, time, comments, number, month, day, year) VALUES //('$date','$blog_content','$user','$time','$comments','$number','$month','$day','$year')");
}
?>
<!-- Include the Free Rich Text Editor Runtime -->
<script src="rte/js/richtext.js" type="text/javascript" language="javascript"></script>
<!-- Include the Free Rich Text Editor Variables Page -->
<script src="rte/js/config.js" type="text/javascript" language="javascript"></script>
<!-- Initialise the editor -->
 
<form action="" method="post">
<script>
<!-- This Must Create the Textarea i am assuming, it hase to either bind to a element, or make its own, it looks like it is making its own, so i put it inside of the form -->
initRTE('this is just some preloaded content, this is just a test tp see where the string will fit and is it fits correctly');
</script>
<input type="submit" name="submit" value="Submit">
</form>
 
<?php
mysql_close ($conn);
?>
</body>
</html>
Read the PHP and HTML Comments.
tomdagom
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 11:32 am

Re: forms and php

Post by tomdagom »

thanks for your reply, your code makes a lot more since.
I am still having problems getting the text from the rte
i can do this using javascript put how i get use this with php is evading me since javascript is client side?
heres how to get the text

document.write(getXHTML(trim(document.getElementById(rteFormName).value)));
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: forms and php

Post by Zoxive »

Where you echo the debug infromation, (echo $temp;)

Echo the $_POST array.

Code: Select all

echo '<pre>';
print_r ($_POST);
echo '</pre>';
That will echo the $_POST array, and one of those should be the Blog Data, then use that name.

We have to go this route, because I never used the Javascript text editor you are enabling, so i don't know what it creates its name to be.
tomdagom
Forum Newbie
Posts: 7
Joined: Wed Jan 16, 2008 11:32 am

Re: forms and php

Post by tomdagom »

ya thats does it. thanks very much for your help.
Post Reply