Need help with my review script!

Discussion in 'PHP' started by Luke Watson, Sep 10, 2010.

  1. #1
    Hi i'm trying to make my own review script but am having lots of trouble. I have done all the html and javascript part but i want it so when a user submits a review it adds it to a database and other users can look at the reviews as well.

    Here is the html/javascript part where users can write a review,

    
    <html>
    <head>
    <title>New Anime Review</title>
    
    <style type='text/css'>
      body { margin:0; padding:0;
      background-color: #000000;
      color: #FFFFFF;
      font-size: 18;
      font-weight: bold;
      text-align: center;
    }
    </style>
    
    </head>
    <body>
    
    <h1>New Anime Review</h1>
    <br /><br /><br />
    <ul>
    <li>Remember to put as much detail into your review as possible.</li>
    <li>Any spam or inappropriate content will not be accepted.</li>
    <li>All fields are required.</li>
    </ul>
    <br /><br />
    <script type='text/javascript'>
    
    function formValidator(){
    	// Make quick references to our fields
    	var anime_name = document.getElementById('anime_name');
    	var anime_authorname = document.getElementById('anime_authorname');
    	var anime_messagearea = document.getElementById('anime_messagearea');
    	
    	// Check each input in the order that it appears in the form!
    	if(isAlphabet(anime_name, "Please enter a name for the anime")){
    		if(isAlphanumeric(anime_authorname, "Please enter your username for the review")){
    			if(isNumeric(anime_messagearea, "Please enter your review")){
    							return true;
    			}
    		}
    	}
    	
    	
    	return false;
    	
    }
    
    function notEmpty(elem, helperMsg){
    	if(elem.value.length == 0){
    		alert(helperMsg);
    		elem.focus(); // set the focus to this input
    		return false;
    	}
    	return true;
    }
    
    function isNumeric(elem, helperMsg){
    	var numericExpression = /^[0-9]+$/;
    	if(elem.value.match(numericExpression)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    
    function isAlphabet(elem, helperMsg){
    	var alphaExp = /^[a-zA-Z]+$/;
    	if(elem.value.match(alphaExp)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    
    function isAlphanumeric(elem, helperMsg){
    	var alphaExp = /^[0-9a-zA-Z]+$/;
    	if(elem.value.match(alphaExp)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    
    function lengthRestriction(elem, min, max){
    	var uInput = elem.value;
    	if(uInput.length >= min && uInput.length <= max){
    		return true;
    	}else{
    		alert("Please enter between " +min+ " and " +max+ " characters");
    		elem.focus();
    		return false;
    	}
    }
    </script>
    
    <form onsubmit='return formValidator()' method="post" action="a_review.php" >
    Name Of Anime:&nbsp; <input type='text' id='anime_name' /><br /><br />
    Review Author:&nbsp; <input type='text' id='anime_authorname' /><br /><br />
    Review: <br /><textarea name="anime_messagearea" rows="12" cols="44"></textarea>
    <br /><br /><br />
    <input type='submit' value='Submit' />
    </form>
    
    </body>
    </html>
    
    Code (markup):
    When the user clicks on Submit it takes them to a php page called a_review.php i'm a noob though at php so i'm not sure how i would do all this so it submits to a database and other users can read reviews as well. Any help would be greatly appreciated.
     
    Luke Watson, Sep 10, 2010 IP
  2. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #2
    You gonna have to show us the code that you have in the a_review.php file.
     
    HuggyEssex, Sep 11, 2010 IP
  3. Luke Watson

    Luke Watson Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for replying. I currently don't have any code in the a_review.php file i'm not sure how to code the rest.
     
    Luke Watson, Sep 11, 2010 IP
  4. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #4
    basically you want us to make the code for you as well as build a structured database for storing the information?
     
    lowridertj, Sep 11, 2010 IP
  5. Luke Watson

    Luke Watson Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You don't have to but if you could tell me how i would get it started, maybe even show me some tutorials that would be much appreciated.
     
    Luke Watson, Sep 11, 2010 IP
  6. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #6
    <?php
    
    //mysql database connection
    $con = mysql_connect("localhost","DB_USERNAME","DB_PASSWORD");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("DB_NAME_HERE", $con);
    
    //pull posted data
    $name = $_POST['name'];
    $anime_authorname = $_POST['anime_authorname'];
    $messagearea = $_POST['messagearea'];
    
    //verify name is not empty if so error
    if($name == ''){ 
           //show error here
           echo "You must provide your name";
           exit();
      }
    
    //verify anime_authorname is not empty if so error
    if($anime_authorname == ''){ 
           //show error here
           echo "You must provide an author name";
           exit();
      }
    
    //verify messagearea is not empty if so error
    if($messagearea == ''){ 
           //show error here
           echo "You must provide a review";
           exit();
      }
    
    //if not one of the areas posted is empty proceed with insert to db
    if($name != '' AND $anime_authorname != '' AND $messagearea !=''){
    
    mysql_query("INSERT INTO reviews (name, anime_authorname, messagearea)
    VALUES ('$name', '$anime_authorname', '$messagearea')");
    
    echo "Thanks for your review.";
    
    mysql_close($con);
    }
    ?>
    PHP:

    You will need to make the Database tables. and a Database.

    TJ
     
    lowridertj, Sep 12, 2010 IP
  7. rockweb

    rockweb Greenhorn

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    Excellent example TJ. thanks for sharing with us.
     
    rockweb, Sep 12, 2010 IP