Jump to content

PHP and JavaScript problem help?


newbe123

Recommended Posts

I have created a web log where you can log in and add new entries, etc.

I'm using PHP and JavaScript. In the page where you can add new entry, I am using JavaScript, but now I want to save these records in a text file that is then displayed on the index.php page. I'm having trouble putting together JavaScript and php code. How can I do that?

 

index.php

 

 

<?php
session_start();

if(isset($_POST['LoutBtn']))
{
session_unset($_SESSION);
session_destroy();
}

if(isset($_POST['LoginBtn']))
{


//convert a string to all lower case letters.
//if user gives username with big letters still can login.
$user = strtolower($_POST['username']);
$pass = $_POST['password'];

if($user == 'admin' && $pass == '123')
{
$_SESSION['LogedIn'] = true;
}

elseif (empty($user) || empty($pass))
         
      
      {
	  print('<font color="#FF0000">Please fill in username and password!<br/></font>');
    
      
      }
	  
elseif ($_POST['username'] != 'admin'){
      
     
      print('<font color="#FF0000">wrong username<br/></font>');
    
      
      }
      
      elseif ($_POST['password'] != '123'){
      
   
      print('<font color="#FF0000">wrong password<br/></font>');

}
}
if (isset($_SESSION['LogedIn']))
if ($_SESSION['LogedIn'] == true)
{
echo 'Welcome You are Loged in.';
?>
    <table width="50"  align="right"  cellpadding="2" cellspacing="2">

<form method="POST" action="panel.php">
<tr>

<td><input type="submit" value="add post" name="PnlBtn" /></td>
</tr>
</form>

<form method="POST" action="stat.php">
<tr>

<td><input type="submit" name="showstat" value="visitorlog" /></td>
</tr>
</form>

</table>  
<?php	

}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form method="post" action="index.php" >

<table width="300" border="1" align="right" cellpadding="2" cellspacing="2">
<tr>
<td width="150">UserName:</td>
<td> <input type="text" name="username" size="20" />
</td>
</tr>
<tr>
<td width="150">Password</td>
<td><input type="password" name="password" size="20" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" name="LoginBtn" />
</td>
</tr>
<tr>
<td><input type="submit" value="Logout" name="LoutBtn" /></td>
</tr>
</table>
<p>

<input type="submit" name="this" value="Current Month" />
<input type="submit" name="prev" value="Previous Month" />
<input type="submit" name="next" value="Next Month" />
</p>
</form>

</body>
</html>

 

 

 

panel.php (where I can add new entry)

 

<?php
session_start();
if   ($_SESSION['LogedIn'] == false)
{
$redirect = "Location: " . $_REQUEST['LoginBtn'] . "index.php";
echo header($redirect);

}

elseif ($_SESSION['LogedIn'] == true)
{
echo "welcome";
}

?>





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
	<script src="bloggadd.js" type="text/JavaScript"></script>		
</head>

<body onload="showdate()">


<form  action="panel.php" method="post">
<p>
			Date (yyyy-mm-dd):<br />
			<input type="text" name="date" id="date" size="12" /><br /><br />

			Subject:<br />

			<input type="text" name="header" id="header" size="12" /><br /><br />				

			Content:<br />
			<textarea name="entry" id="entry" rows="20" cols="40"></textarea><br /><br />	

			<input type="submit" name="add" value="add post" onclick="return validate()"/>

		</p>



</form>



</body>
</html>
<?php
$page_parts = explode("/", $_SERVER['PATH_INFO']);

$page_filename = $page_parts[count($page_parts)-1];



$lastpage = $_SESSION['thispage'];

if ( strlen ($lastpage) == 0 ) $lastpage = "index.php";



$_SESSION['thispage'] = $page_filename;

?>
<form  action="<?php echo $lastpage ?>" method="post">
<table>
<tr><td clospan="2"><input type="submit" value="Home" name="back" /></td></tr>
</table>
</form>

 

 

bloggadd.js (the JavaScript code)

 

 

// Show current date in the date box
function showdate()
{
var now = new Date();

document.getElementById('date').value = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
loginform.header.focus();
}

// Validate Form
function validate()
{	
var date = document.getElementById('date').value;
// Heading is no requirement
var entry = document.getElementById('entry').value;	

// If the date is note the correct format
if (!/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(document.getElementById('date').value)) 
{    
  		alert('Please enter the dates in the format yyyy-mm-dd.');  	
  		document.getElementById('date').focus();
  		
  		return false;
}	

// on missing posts
if (entry.length == 0)
{
	alert("Fyll i ett inlägg.");
	document.getElementById('entry').focus();

	return false;
}

return true;				
}

 

 

 

I really appreciate if someone helps me.

Link to comment
Share on other sites

No. no errors.

 

The problem I am having is that I have created this blog and I have used JavaScript and now when I add a new post I want to save it on a file and than display  the saved posts on index.php.

 

I just don't know how to do that. I don't know how to use JavaScript codes in php.

Link to comment
Share on other sites

I just don't know how to do that. I don't know how to use JavaScript codes in php.

 

That's because you don't use JavaScript in PHP. Nothing you are asking for requires JavaScript.

 

In any event, this forum is for people who want "help" with code they have written. I think you should be posting in the freelance forum.

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.