Jump to content

Passing JS value to PHP


eatfishy

Recommended Posts

I haven't worked much with JS, so I was wondering if anyone can help me out.

 

<?php
include("Session.php");
include("Menu.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>BlogoBerry - Create your free blog or online diary</title>
<meta name="verify-v1" content="XkVeFYClT7cujF+i4PkEOk07c+x03VY1uy5oRLDs8k0=" >
<meta name="keywords" content="blog, blogger, online diary, social network, voice opinion, poll, contest, creative writing, free website" />
<meta name="description" content="Create a free blog, online diary, weblog, voice your opinion, view poll, meet new friends or simply call it Blogoberry!" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script language="JavaScript" type="text/javascript" src="richtext.js"></script>
</head>
<body>
<!-- start header -->
<?php include("Header.php"); ?>
<!-- end header -->
<!-- start menu -->
<div id="menu">
        <ul>
            <table width="850px">
                <tr valign="middle">
                    <td>
                        <?php echo $leftmenudisplay; ?>
                    </td>
                    <td align="right">
                        <?php echo $rightmenudisplay; ?>
                    </td>
                </tr>
            </table>
        </ul>
</div>
<!-- end menu -->
<!-- start page -->
<div id="page">
<!-- start content -->
<div id="content_b">
	<div class="post">
		<div class="title">
			<h2>New Blog</h2>
			<div id="blogfooterlink"><table width="100%"><tr valign="top"><td align="left" valign="middle"><p><small>Write your title and content below.</small></p></td><td align="right" valign="top"><img src="images/content/return.png" alt="" /> <a href="mypicture.php">Return to My Account</a></td></tr></table></div>
		</div>
		<div class="entry">
			<p>
                                <form id="newblogform" method="post" onsubmit="return submitForm();">
                                    <script language="JavaScript" type="text/javascript">
                                function submitForm()
                                {
                                    //make sure hidden and iframe values are in sync before submitting form
                                    updateRTE('rte1'); //use this when syncing only 1 rich text editor ("rtel" is name of editor)
                                    //updateRTEs(); //uncomment and call this line instead if there are multiple rich text editors inside the form
                                    //alert("Submitted value: "+newblogform.rte1.value); //alert submitted value
                                    true; //Set to false to disable form submission, for easy debugging.
                                }
                                //Usage: initRTE(imagesPath, includesPath, cssFile)
                                initRTE("images/richtext/", "", "");
                           </script>
                                    <table>
                                            <tr>
                                                <td><b>Title</b></td>
                                            </tr>
                                            <tr>
                                                <td><input id="title" name="blogtitle" type="text" size="40" maxlength="40" value="<?php echo $_SESSION['blogtitle'];?>"></td>
                                            </tr>
                                            <tr>
                                                <td>
                                                       <script language="JavaScript" type="text/javascript">
                                            //Usage: writeRichText(fieldname, html, width, height, buttons, readOnly)
                                            //writeRichText('rte1', '', 760, 300, true, false);
                                            //var test=document.getElementById(rte1);
                                            writeRichText('rte1', '<? echo $_SESSION["rte1"]; ?>', 760, 300, true, false);
                                            //-->
                                        </script>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <input type="submit" name="postsubmit" value=" Post" size="20"/><font style="color:red; font-weight:bold;"><?php echo $_SESSION['error'];?></font>
                                                </td>
                                            </tr>
                                        </table>         
                                    </form>
                                </p>
                        </div>
	</div>
</div>
<!-- end content -->
<div id="extra" style="clear: both;"> </div>
</div>
<!-- end page -->
<!-- start footer -->
<div id="footer">
<p class="legal"></p>
</div>
<!-- end footer -->
</body>
</html>

<?php
    if (isset($_POST['postsubmit']))
    {
        if(isset($_SESSION['username']))
        {
            $_SESSION['blogtitle']=ltrim($_POST['blogtitle']);
            $_SESSION['blogcontent']=$_POST['rte1'];

            if($_SESSION['blogtitle']<>'')
            {
                if($_SESSION['blogcontent']<>'')
                {
                    include("MySQLconnect.php");
                    //Insert new blog
                    mysql_query("INSERT INTO `blog` (`memberid`,`blogdatetime`,`blogtitle`,`blogcontent`) VALUES('{$_SESSION['memberid']}',NOW(),'{$_SESSION['blogtitle']}','{$_SESSION['blogcontent']}');") or die(mysql_error());

                    $recentblog=mysql_query("SELECT * FROM `blog` WHERE memberid='".$_SESSION['memberid']."' ORDER BY blogid DESC LIMIT 0,1;");
                    $recentblogcolumn=mysql_fetch_assoc($recentblog);

                    //Insert into commmunity's recent activities
                    $action='<div id="blogfooterlink"><a href="/blogmember.php?username='.$_SESSION['username'].'">'.$_SESSION['username'].'</a> wrote a blog called <a href="/blogdetail.php?username='.$_SESSION['username'].'&blogid='.$recentblogcolumn['blogid'].'">'.$_SESSION['blogtitle'].'</a></div>';
                    mysql_query("INSERT INTO `community` (`avatar`,`action`,`createdate`) VALUES('{$_SESSION['avatar']}','{$action}',NOW());") or die(mysql_error());

                    header("Location: /myaccount.php");
                }
                else
                {
                    $_SESSION['error']=' You must write something in your blog.';
                    header("Location: /newblog.php");
                }
            }
            else
            {
                $_SESSION['error']=' You must write a title for your blog.';
                header("Location: /newblog.php");
            }
        }
        else
        {
            header("Location: /memberonly.php");
        }
    }
    else
    {
        $_SESSION['error']='';
        $_SESSION['blogtitle']='';
        $_SESSION['blogcontent']='<script language="JavaScript" type="text/javascript">document.getElementById(rte1).value;</script>';
    }

?>

 

I am trying to get $_SESSION['blogcontent'] to store a value from javascript. I want to let user to write a blog, click on other links on my website, then come back to the page and it'll still show what they wrote until it's submitted. Below is a login account to www.blogoberry.com

 

username: test

password: test

 

1) Login

2) Click on Write New Blog

3) Write something there

4) Click on other links

5) Click on your account (upper right hand )

6) Click on Write New Blog

7) Still show the content of blog that was written previously

 

I appreciate any help..

Link to comment
Share on other sites

the only way that you are going to be able to get a value out of JavaScript (which runs on the client) back into PHP (which runs on the server) is by a page load.

 

Here is an example:

 

<script language="JavaScript">
var Result = 55;
location.href="myPage.php?Result=" + Result;
</script>

Javascript loads Before PHP

so the variable would always come out as nothing.

 

this was researched from google.

Link to comment
Share on other sites

Javascript loads Before PHP

so the variable would always come out as nothing.

 

No PHP loads before JavaScript, hence you need to reload the page while passing the value via GET..

 

IMO: I would have a onchange in the textarea that saves the this.value to a cookie, you could fire off a ajax request but its simpler to just use a cooke, you could then set the value of the textarea to the cookies value, or if you really wanted you could have PHP save it to a session but i don't really see the point.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.