Jump to content

Javascript integration with PHP problem


Noskiw

Recommended Posts

<?php

class modernCMS {
    
    var $host;
    var $username;
    var $password;
    var $db;
    
    function connect(){
        $con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
        mysql_select_db($this->db, $con) or die(mysql_error());
    }
    
    function get_content($id = ''){
        $id = mysql_real_escape_string($id);
        if($id != ''){
            $sql = "SELECT * FROM cms_content WHERE id='" . $id . "'";
            $return = "<a href='index.php'>Go back to content</a>";
        }else{
            $sql = "SELECT * FROM cms_content ORDER BY id DESC";
        }
        $res = mysql_query($sql) or die(mysql_error());
        if(mysql_num_rows($res) != 0){
            while($row = mysql_fetch_assoc($res)){
                echo "<h1><a href='index.php?id=" . $row['id'] . "'>" . $row['title'] . "</a></h1>";
                echo "<p>" . $row['body'] . "</p>";
                echo $return;
            }
        }else{
            echo "<p>ID for post does not exist!</p>";
        }
    }
    
    function manage_content(){
        echo '<div id="manage">';
        $sql = "SELECT * FROM cms_content";
        $res = mysql_query($sql) or die(mysql_error());
        if(mysql_num_rows($res)==0){
            echo "There are no posts. <a href='add-content.php'>Add one now!</a>";
        }
        while($row = mysql_fetch_assoc($res)){
            echo"
<div>
<h2 class='title'>" . $row['title'] . "</h2>
<span class='actions'><a href='update-content.php?id='" . $row['id'] . "'>Edit</a> | <a href='?delete=" . $row['id'] . "'>Delete</a></span> 
</div>";
        }
        echo "</div>";
    } 
    
    function delete_content($id){
        if(!$id){
            return false;
        }else{
            $id = mysql_real_escape_string($id);
            $sql = "DELETE FROM cms_content WHERE id = '" . $id . "'";
            $res = mysql_query($sql) or die(mysql_error());
            echo "Content Deleted Succesffuly";
        }
    }
    
    function add_content(){
        if(!$_POST['submit']){
            echo "<form action='add-content.php' method='POST'>";
            echo "<tr><td><input type='text' name='title' /></td></tr><br /><br />";
            echo "<tr><td><textarea name='body' width='140' height='40'></textarea></td></tr><br />";
            echo "<tr><td colspan='2'><input type='submit' name='submit' value='Post' /></td></tr>";
            echo "</form>";
        }else{
            $id = $_POST['id'];
            $title = mysql_real_escape_string($_POST['title']);
            $body = mysql_real_escape_string($_POST['body']);
            
            $sql = "INSERT INTO cms_content (id, title, body) VALUES('".$id."','".$title."','".$body."')";
            $res = mysql_query($sql) or die(mysql_error());
            echo "Row successfully entered";            
        }
    }
    
}

?>

 

I would like to add some javascript to make you enter a password to view a page. But I have a method in javascript. Unfortunately, when I try to add it, if you click cancel, you can get on the page anyway. Here is my code.

 

<script type="text/javascript">

var password = "nomnom";

function prtyo(){
    var p = prompt("Enter password");
   
    if(p != password){
        var cancel = confirm("Wrong password, do you want to try again?");
        if (cancel){
            prtyo();
        }
    }else{
        window.location = "add-content.php"; 
    }
}

prtyo();
</script>

 

I would add it here so it looks like

 

<?php
   function add_content(){
?>
<script type="text/javascript">

var password = "nomnom";

function prtyo(){
    var p = prompt("Enter password");
   
    if(p != password){
        var cancel = confirm("Wrong password, do you want to try again?");
        if (cancel){
            prtyo();
        }
    }else{
        window.location = "add-content.php"; 
    }
}

prtyo();
</script>
<?php
        if(!$_POST['submit']){
            echo "<form action='add-content.php' method='POST'>";
            echo "<tr><td><input type='text' name='title' /></td></tr><br /><br />";
            echo "<tr><td><textarea name='body' width='140' height='40'></textarea></td></tr><br />";
            echo "<tr><td colspan='2'><input type='submit' name='submit' value='Post' /></td></tr>";
            echo "</form>";
        }else{
            $id = $_POST['id'];
            $title = mysql_real_escape_string($_POST['title']);
            $body = mysql_real_escape_string($_POST['body']);
            
            $sql = "INSERT INTO cms_content (id, title, body) VALUES('".$id."','".$title."','".$body."')";
            $res = mysql_query($sql) or die(mysql_error());
            echo "Row successfully entered";            
        }
    }
    
}

?>

 

Unfortunately, it makes you re-enter the password over and over and over again. Which gets increasingly annoying each time. Any help would be apreciated ;)

Link to comment
Share on other sites

Again, please do not post any code that are not relevant to the problem, like that PHP class file. If I haven't helped you earlier, I would have no idea what that class does or why you posted it. It doesn't help us help you if you post up more code that are not of any relevance.

 

You shouldn't have JavaScript be a validation to get into a page. It just doesn't work, period.

Link to comment
Share on other sites

Are there any other methods that I could use? And the class file is basically the foundation of a specific page.

 

I'll post one of my main pages.

 

<?php
include 'class/cms_class.php';

$obj = new modernCMS();

//Setup Our Connection Vars
$obj->host = 'localhost';
$obj->username = 'root';
$obj->password = '';
$obj->db = 'modernCMS';

//Connect To Our DB
$obj->connect();

?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UFT-8" />
<title>My Modern CMS</title>
<link href="./style.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div id="page-wrap">
    <?php include'nav.php'; ?>
        <?php if(isset($_GET['id'])):
            $obj->get_content($_GET['id']);
        else:
            $obj->get_content();
        endif;
        ?>
</div>
</body>
</html>

 

That basically sets up the page and posts what is on there by using $obj->get_content(); This makes it easier than typing in the whole page on one document and makes it look a lot cleaner.

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.