Jump to content

WillUK

Members
  • Posts

    17
  • Joined

  • Last visited

Everything posted by WillUK

  1. <HTML> <head> <title>Exam entry</title> <script language="javascript" type="text/javascript"> function validateForm() { var result = true; var msg=""; if (document.ExamEntry.name.value=="") { msg+="You must enter your name \n"; document.ExamEntry.name.focus(); document.getElementById('name').style.color="red"; result = false; } if (document.ExamEntry.subject.value=="") { msg+=”You must enter the subject \n"; document.ExamEntry.subject.focus(); document.getElementById('subject').style.color="red"; result = false; } if(msg==""){ return result; } { alert(msg) return result; } } </script> </head> <body> <h1>Exam Entry Form</h1> <form name=”ExamEntry" method="post" action="success.asp"> <table width="50%" border="0"> <tr> <td id="name">Name</td> <td><input type="text" name="name" /></td> </tr> <tr> <td id="subject">Subject</td> <td><input type="text" name="subject" /></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td> <td><input type="reset" name="Reset" value="Reset" /></td> </tr> </table> </form> </body> </html> Hi, I am new to JavaScript and am trying to apply the above form validation code. Would an experienced JavaScript developer please take a look and let me know why it is not processing? Am I missing some code? Calling it incorrectly? I appreciate that this is pretty basic stuff but it is driving me nuts! Info: success.asp does not yet exist. Any help would be really appreciated. Thanks Will
  2. Thanks Haku - I considered your approach, but wouldn't I need to somehow apply an ID to it in order to be able to call it from inside the html? At the moment I've got in my html file: (This is in the head section) <script type="text/javascript" language="Javascript" src="javascript/cookie.js"> </script> Then just above the closing body tag: <div id="gas"> </div> Are you saying that I don't need to assign an id?
  3. Hi I have recently taken up Javascript after a couple of years away from coding, so am finding this pretty tough to get my head around. What I am trying to do is to set up the Google Analytics tracking code in an external Javascript file. I do not want the analytics code to be outputted/set on the page unless the user chooses to accept it (This is relating to recent changes to the cookie legislation.). If the user does not choose to accept Analytics, the script runs without setting the analytics code. I decided to use an event handler to deal with this, and I have stored the anlaytics code itself in a function, which is then passed to another function - disp_confirm() - in order for it to have an id applied to it. The plan then is to call this id (and therefore call the event handler) from the just above the closing body tag in the html page (I decided against putting the analytics code in the header because I was under the impression that the event handlers wouldn't process successfully if the code was implemented here). The problem is, I don't know whether this will work, or if I am trying to do something that cannot be done with Javascript. The idea of passing a function to another function doesn't sit well with me - so I'm sure I must be way off the mark with this....I'm not sure if I'm using the correct syntax to pass one function to another either Anyway, here's my code. I'd really appreciate some advice on this, especially if I am way off mark with my approach!! Thanks window.onload = disp_confirm; function disp_confirm() { var r=confirm("This website uses Google Analytics to monitor website use which helps us offer a better service to you. However, No personal data is stored. If you wish to proceed with cookies, click ok. Alternatively click cancel.") if (r==true) { var gas = passString.analytics(); document.getElementById('gas'); } else { alert("Google cookies have been disabled") } } function analytics() { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXX-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); }); } Any help advice would be really appreciated.... Thanks
  4. Hi MattyB I have a very similar problem. I am pulling 'categories' from a database e.g. sport, current affairs etc etc. Each category is then listed individually as a link. When the user clicks a category link, the user is redirected to a page: blog.php . blog.php then displays all the threads for that particular category. Well that's what should happen anyway - I just can't figure out how to store the category id and then communicate that info to the seperate blog.php file. I thought that storing the data using sessions might be a way forward, but that proved to be a dead end. The session data was stroring category info, but the same category id was being strored irrespective of the category selection being made i.e. the user selects 'current affairs' but the displayed data is 'sport'; the user selects 'politics, but the displayed data is 'sport' etc etc So, I'm a bit stuck too. If I manage to find a solution I will post it back in here: but at least you know you are not alone!
  5. Thanks. Your answer is a bit advanced for me, but I'll try to get my head around it. For your information, I am quite new to object oriented programming and am using a format found within a tutorial I referred to. If it isn't any good, don't blame me. I am just learning. But thanks for your advice
  6. Thanks for that but I don't get why?!! I've put the line: public $blog_categoryName = NULL; This has been put at the top the class, before any methods have been created. I've also put the line just to make sure, by giving the variable a value: if(!isset($blog_categoryName)) { $blog_categoryName = 'value'; } So I have set the variable haven't I??
  7. Hi I am buidling a Blog System using OO techniques (or at least attempting to).. I keep getting an error : Notice: Undefined variable: blog_categoryName in C:\wamp\www\atkinsonsCMS\admin170976\blog_categories.php on line 13 ...And then underneath all that is is displaying 'Call Stack'. I've never come across this notification before so I guess it is an OO term, and the a list of unexpected outputs: #, time, memory, function and location....each with an associated value. I attempted to resolve the variable issue in the usual way (as you can see in my code), but still the errors/warnings persist. The purpose of this part of the code is to simply retrieve SQL data (which is a list of blog categories), allowing the user to select one and then click through to be directed to the appropriate category page (although this last requirement of the code has not yet been programmed). Could those who have experience of this please take a look at my code and let me know where I am going wrong? Thanks blog_categories.php This file calls on the the blogFunctions.php Category Class, and the displays the results. session_start(); $pageTitle = "Blog Categories"; include("../includes/admin_header.php"); include("../classes/blogFunctions.php"); echo '<div class="admin_main_body"><br /><br />'; echo "<h2>Category List</h2><br />"; $content = new Category; $content -> process(); //print_r(debug_backtrace()); echo "<a href=blog.php>$blog_categoryName</a>"; echo '</div>'; echo '</div>'; include("../includes/admin_footer.php"); blogFunctions.php This class deals with the retreaval of the SQL data, and then passes it to blog_categories.php class Category { public $errors; public $blog_categoryName = NULL; public function __construct() { $this->errors = array(); $blog_categoryName = $this->blog_categoryName; } public function getCategories(){ if(!isset($blog_categoryName)) { $blog_categoryName = 'value'; } ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "atkinson", "XYZ111WA")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE atkinsonscms")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM blog_category ORDER BY ASC"); $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); if ($result) { while ($row = $result->fetch_object()) { $blog_categoryName = $row->blog_categoryName; } //need to use mysqli query to retrieve results. if(mysqli_affected_rows($GLOBALS["___mysqli_ston"])< 1) $this->errors[] = 'Could not retrieve data'; } } public function show_errors(){ echo "<h3>Errors</h3>"; foreach($this->errors as $key=>$value) echo $value."<br>"; } public function process() { $this->getCategories(); return count($this->errors)? 0 : 1; } } //Closes Category Class.
  8. Eureka! I've done it! It was nothing to do with the logic. It was to do with where I was putting the quotation marks in the table! The use of var_dump pinpointed the problem!! Thus I changed the respective code in displayUsers() to: $email = $row->email; $username = $row->username; $id = $row->admin_id; echo "<tr><td>$username</td><td>$email</td><td><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id /></td></tr>"; Damn quotation marks. They are a the bane of my development life! Now to look at the Zend Framework!
  9. Hey... I'm back again...with the same problem 'dressed differently'. I gave up with the previous effort, and went back to the drawing board. I attempted to put into place some of your advice about breaking down classes and functions into smaller modules/components, but I think that the main problem I am having is getting the functions in the same class to 'talk to each other' properly.... For example: the sql data (users) is being pulled from the database and outputted to screen. So like before - this part of the application works fine. The function to carry out this work is called showUsers(). Instead of then putting the code to carry out the 'delete' process in the same function, I created a new one: removeUsers(). So - I then wanted to pass both the $_POST['delete'], and the $_POST['checkbox'] values to the removeUsers() function. Both of these variables/attributes were set in showUsers(). $_POST['delete'] contains the delete button data for the form. $_POST['checkbox'] contains an array of data, populated by the check boxes. However, this is where it breaks down. Now, when I click on 'delete users', the user list remains in view, but no users are deleted. At the bottom of the list I am getting the following output: 'Error: Form not processed'. This was an error checking line that I added within the if isset() conditional I created to check for errors....One of the confusing/fascinating issues here is that php must be recognising that the delete button has been pressed because I contained the error check WITHIN the if isset() conditional...So it MUST have passed that condition...then it is running through the code and then it breaks up somewhere and decides not to work.... If the delete event hadn't taken place, the code would have outputted "The form could not be processed": the string which is the 'else' part of the isset() conditional. Ok - so the problem is somewhere else in the removeUsers() function. I then used var_dump($checkbox) to see whether checkbox contains any data. $checkbox is the variable that the value of $_POST['checkbox'] was passed to in the removeUsers() function. Var dump gave me the following output when I checked 4 users for deletion: array(4) { [0]=> string(4) ".18." [1]=> string(3) ".8." [2]=> string(4) ".24." [3]=> string(4) ".15." } Error: form not processed The output is saying that 4 users were selected, with user_ids 18, 8, 24 and 15 respectively.... I don't understand what string(4), string(3), and string(4) means however... The final part of the output is saying that the form was not processed, which is the internal error output for the if isset condition that I created..... Ok - I haven't a clue what to do now. Clearly the values are being passed to the removeUser() function, but then they are not being passed to the SQL query for deletion. I've retained the var_dump line in order to give the error message I received above more clarity - but clearly this should be discounted from the final code.... Any ideas?? Here's the code: class userManagement { function removeUsers() { //$userManagement->showUsers(); //if (showUsers()) { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); if(isset($_POST['delete'])) // from button name="delete" { //echo $_POST['checkbox']; $checkbox = $_POST['checkbox']; //from name="checkbox[]" //echo $checkbox; var_dump($checkbox); $countCheck = count($_POST['checkbox']); //var_dump($countCheck); for($i=0;$i<$countCheck;$i++) { $del_id = $checkbox[$i]; $query = "DELETE FROM admin WHERE admin_id = $del_id"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); } if($result) { header('Location: admin_index.php'); } else { echo "Error: form not processed"; } } else { echo "The form could not be processed"; } } function showUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); $query = "SELECT admin_id, username, email FROM admin"; //$result = @mysqli_query($GLOBALS["___mysqli_ston"], ($query)or die(mysqli_error($connection))); $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); //$result = $connection->query($query) or die(mysqli_error($connection)); if ($result) { //create a new form and then put the results into a table. echo "<table cellspacing='0' cellpadding='10'>"; echo "<tr><th colspan='4'>List of Current Users</th></tr>"; //echo "<tr><th width='25%'>Username</th><th width='25%'>Email Address</th><th width='25%'>Delete</th></tr>"; echo "<tr><th>Username</th><th>Email Address</th><th>Select Users</th></tr>"; while ($row = $result->fetch_object()) { //$this->email = $row->email; //$this->username = $row->username; //$this->admin_id = $row->admin_id; $email = $row->email; $username = $row->username; $id = $row->admin_id; echo "<tr><td>".$username."</td><td>".$email."</td><td><input type='checkbox' name='checkbox[]' id='checkbox[]' value='.$id.'/></td></tr>"; } echo "</table>"; echo "<p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Users'/></p></form>"; } else { echo "The user list could not be displayed due to a technical error. Please consult the administrator."; } } } class database_connection { public function connection() { $db_name = "practicesite"; $db_server = "localhost"; $db_user = 'root'; $db_pass = ''; } //connection($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error()); } $connection = new database_connection($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error()); As you can see, I'm in the process of modulating the SQL connection, by putting it in its own class. I initially tried to implement(instantiate) it in this script - but had a problem with calling an object on a non object (or something or other like that).
  10. Thank you I'll try to objectify the database connection using your tip! I am worried that if I run before I can walk, I'll end up making numerous mistakes and then not be able to rectify them. If I can get the script working, I can then re-examine the class and break it up accordingly. Although based on what you have said about creating a new function for the database connection, it looks reasonably simple to achieve. I'll take a look at Zend and see what they offer. Although this is my first 6 weeks of OO PHP so I'll probably find it pretty difficult to figure out.... Ok - I'm trying to output the data using an echo statement at the following point: if (count($_POST) > 0 ) { $userDelcount = 0; foreach($_POST as $name => $value) { echo $name; } I also tried echo "$name"; (because the values should be strings).... Nothing at all is happening however. So this would suggest that no form isn't communicating with the database correctly. Although clearlly the database is being recognised earlier on in the script, because it is outputting the users to screen in the table.... I'll have a another go by changing the action attribute to action = "" . I noticed that on the procedural script that I was using as a framework for this - the action attribute was set as follows: action="remove_user.php?<?php echo action="getdel" ?;>" This may be irrelevent, yet the procedural script works fine. That's why I thought that the error that I am making could well be to do with the form action attribute that I am setting. Come to think of it - what does the line <?php echo action="getdel" ?> actually mean? 'getdel' isn't a variable (I've checked the script a few times to find a reference to it).....There is no reference to it elsewhere in the script. Anyway - it is probably irrelevent! But has been baffling me. All I know is that with the procedural script, when I removed the action=godel code, the script failed to run.... I'll keep examining!
  11. I've made a number of amendments to the scripts, so I've attached an updated version. When I click on delete, the form still fails to process....It must be something to do with the action attribute...???? remove_user.php session_start(); $pageTitle = "Remove User"; include("../includes/admin_header.php"); include("../classes/userAuthorisationFunctions.php"); echo '<div class="admin_main_body">'; echo '<br /><br />'; $userManagement = new userManagement(); <form name="userList" action="<?php htmlentities($_SERVER["PHP_SELF"]) ?>" method="post"> $userManagement->removeUsers(); echo '<form>'; echo '<div>'; include("../includes/admin_footer.php"); userAuthorisation.php class userManagement { function getUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); $query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($query)) { echo '<table><tr><th colspan="1">Username</th><th colspan="1">Email Address</th></tr>'; echo '<tr class="yellow"><td class="width">' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td></tr>'; echo '</table>'; } } function removeUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); //if (isset($_POST['submit'])){ if(!isset( $_REQUEST['action']) ) { $sql = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($sql)) { echo '<table>'; echo '<tr class="yellow"><td class="width">Username</td><td class="adjacent">Email Address</td><td class="adjacent"></td><td class="adjacent"></td></tr>'; echo '<tr><td>' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td><td class="adjacent"><input type="checkbox" name=' . $user['username'] . '/></td></tr>'; echo '</table>'; } echo '<input type="submit" value="Delete Selected User(s)"/>'; } else if(isset($_REQUEST['action'])) { if (count($_POST) > 0 ) { $userDelcount = 0; foreach($_POST as $name => $value) { $query = "DELETE * FROM admin WHERE where username = '$name'"; //$result = mysqli_query($GLOBALS["___mysqli_ston"], $query); if (@mysqli_query($GLOBALS["___mysqli_ston"], $query)); //if ($result) { $userDelcount ++; } } } else if (count($_POST) != $userDelCount ) { echo "<p>There was a problem processing your request.Please try again later.</p>"; } else { echo "<p>The user(s) you selected has/have been deleted.</p>"; unset($_REQUEST['action']); echo "<p>Please <a href = '../admin170976/remove_user.php'>click here to continue.</p>"; } } else echo "<p>You did not select any users.</p>"; //"<p>Please <a href = '../admin170976/remove_user.php'>click here to return.</p>"; } }
  12. I've decided to post my code, because I'm having no luck with this I have two files: 1/remove_user.php - this instantiates the class (userManagement) and calls the method (removeUsers). It also contains the html form opening and closing elements (the opening element is clean html. The closing element is contained within PHP tags). 2/userAuthorisationFunctions.php - this contains the class which contains the removeUsers method. - this class also contains the getUsers method, but this is not being used, and it is probably redundant. The concept is as follows: I am using a form to display user data within a CMS Administrator environment. I want the administrator to be able to remove users accordingly. I am using checkboxes, whereby the administrator can select multiple users to delete if necessary. I am using a while loop to store the SQL data pulled from the database. In the first instance, the user data is displayed. This part of the method works fine and displays the data correctly. A delete button has been added outside of the loop because I want the user to to be able to check multiple boxes, but only be required to action the delete call once to remove them (rather than deleting one user at a time, which would have been the case had I stored the delete button within the loop). When I select a user, and click on delete, nothing happens. This leads me to believe that there is something wrong with the action attribute that I have set. Here's the code, and thanks for any help you can give me. remove_user.php session_start(); $pageTitle = "Remove User"; include("../includes/admin_header.php"); include("../classes/userAuthorisationFunctions.php"); echo '<div class="admin_main_body">'; echo '<br /><br />'; $userManagement = new userManagement(); <form name="userList" action=" htmlentities($_SERVER["PHP_SELF"]) " method="post"> $userManagement->removeUsers(); echo '<form>'; echo '<div>'; include("../includes/admin_footer.php"); userAuthorisationFunctions.php class userManagement { function getUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); $query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($query)) { echo '<table><tr><th colspan="1">Username</th><th colspan="1">Email Address</th></tr>'; echo '<tr class="yellow"><td class="width">' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td></tr>'; echo '</table>'; } } function removeUsers() { ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost", "root", "")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE practicesite")) or die (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); //if (isset($_POST['submit'])){ if(!isset( $_REQUEST['action']) ) { $sql = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM admin ORDER BY username ASC"); while ($user = mysqli_fetch_assoc($sql)) { echo '<table>'; echo '<tr class="yellow"><td class="width">Username</td><td class="adjacent">Email Address</td><td class="adjacent"></td><td class="adjacent"></td></tr>'; echo '<tr><td>' . $user['username'] . '</td><td class="adjacent">' . $user['email'] . '</td><td class="adjacent"><input type="checkbox" name=' . $user['username'] . '/></td></tr>'; echo '</table>'; } echo '<input type="submit" value="Delete Selected User(s)"/>'; } else if(isset($_REQUEST['action'])) { if (count($_POST) > 0 ) { $userDelcount = 0; foreach($_POST as $name => $value) { $query = "DELETE * FROM admin WHERE where username = '$name'"; echo $query; $result = mysqli_query($GLOBALS["___mysqli_ston"], $query); if ($result) { echo $result; $userDelcount ++; } } } else if (count($_POST) != $userDelCount ) { echo "<p>There was a problem processing your request.Please try again later.</p>"; } else { echo "<p>The user(s) you selected has/have been deleted.</p>"; unset($_REQUEST['action']); echo "<p>Please <a href = '../admin170976/remove_user.php'>click here to continue.</p>"; } } else echo "<p>You did not select any users.</p>"; //"<p>Please <a href = '../admin170976/remove_user.php'>click here to return.</p>"; } } Thanks!
  13. Hi I am building a class to manage site users (Via a CMS). In order to complete this I am building a delete/remove user function/method. Things are going ok (I think), but I am confused about what to put in the action tag of the form being used to process the data. Before coding my method, I referred to a script that someone showed me which does something similar in functionality: it deletes photos previously uploaded to a folder, and also removes the associated information from the database. I used this script as the framework for the code that I am working with. Eveything seems to be working fine - EXCEPT - when I click on the 'delete users' button, nothing happens. The first thing that I examined for error was the action attribute within my form. The value of the action attribute is: action = "<? php htmlentities($_SERVER["PHP_SELF"]) ?>" This format has worked for previous forms. I compared this to the action attribute within the script that I was using as a guide. It's value is set as follows: action = "remove_photo.php?<?php echo "action='godel' ";?> In this instance, remove_photo.php is the file that is being used to both process and display the form. The reference script works perfectly but it uses a procedural approach . For my application, I am using a more object oriented approach, so both my opening and closing form tag elements are stored in a seperate file to the class file that processes it. I am new to OO but relatively comfortable with the paradigm so far (my other methods etc are working fine). I therefore suspect that the reason for my script's inability to process is due to the action attribute setting (I cannot be 100% sure of course!). I am going to therefore apply the action attribute settings used successfully in the reference script, to the application that I am building. However, the trouble is, I don't understand it I don't like it when something works, but I can't see the logic behind why it does. I am not concerned about the form id or method attrbutes. I understand what is going on there - and why they are set as they are. However, the action attribute is very difficult to understand for the following reasons: 1/ There is no variable/method/entity/object/ANYTHING called 'godel' elsewhere in the reference script. For this reason I thought that it might not be needed, so removed the bit of code within PHP tags concerning it: <?php echo "action='godel' ";?> - When I did this - the script didn't work. In fact - nothing at all would process. This was a similar result to what was happening with the script I am building. 2/ I then put the PHP line back in, but replaced 'godel' with 'something'. I saved and processed the form. - it worked. 3/ I then tried it again replacing 'godel' with an empty string i.e: ' '. - it worked. Has anyone come across forms being processed in a similar way before? If so, please could you explain what is happening here. I can't find anything relating to this type of occurence online.... I am going to place the php tags in the file I am building, to see if it has the desired effect. If it does, I will obviously keep it. But not understanding what is happening, or why it is happening is very annoying!! Thanks
  14. I've solved it! Although I'm not entirely sure why the amendment was necessary.... I made the database connection ($dbc) global within the mysql_connect script. Then everything seemed to work fine. When I was using the procedural approach this was never an issue, thus using the procedural approach ($dbc) did not have to be global. When I was trying to run the script prior to setting it as global I was getting the error "parameter 1 expected to be mysqli, null given" (or something like that). Confusing! That's programming I guess: not my favourite part of the job! Anyway, thanks for your help!
  15. Hi Guys Thanks for your help. I am working on building classes, breaking them down and modulating them. But the way I see it: I need to get one thing working first lol But notice that I've altered the name of the class itself to 'login', which is certainly more modular than siteFunctions.... Anyway - I've instantiated the class, and called the admin_login method as suggested: but it still isn't working properly... However, the error I am now getting suggests that there is a 'parse error' on line 4 of sitefunctions.php. However - maybe i'm blind - but I can't see anything wrong with the syntax... Here are my updated scripts: Admin Login Form (index.php) $pageTitle = "Admin Login"; include("admin_header.php"); include_once("sitefunctions.php"); $login = new $login(); //Changed the name of the class to be more modular. Instantiates the login class. $login->admin_login(); //Calls the admin login function. echo '<div class="admin_main_body">'; [/php> <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]); ?>" method='post'> <!--echo $_SERVER["PHP_SELF"];--> <input type="text" name="username" id="username" size="20"> <label>Username</label><br /> <input type="password" name="password" id="password" size="20"> <label>Password</label><br /> <input type="submit" name="submit" id="submit" value="submit"> </form> [code=php:0] echo '<div>'; include("admin_footer.php"); Site Functions (sitefunctions.php) class login { var $message = NULL; var $username = $_POST['username']; //Parse error here?!!!!! var $password = $_POST['password']; function admin_login() { echo '<div class="admin_main_body">'; $message = NULL; if (isset($_POST['submit'])) { require_once ("mysql_connect.php"); if (empty($username)) { $username = FALSE; $message .= '<p> Please enter your username </p>'; } else { $username = TRUE; $username = escape_data($username); } if (empty($password)) { $password = FALSE; $message .= '<p>You forgot to enter your password </p>'; } else { $password = TRUE; $password = escape_data($password); } if ($username && $password) { // If everything's OK. $query = "SELECT * FROM admin WHERE username= ('$username') AND password=('$password')"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); $row = mysqli_fetch_array($result, MYSQLI_BOTH); if ($row) { session_start(); $_SESSION["admin_id"] = $row[0]; //header("$page"); //Redirects user to admin_index.php //header('location: "$page"'); header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "admin_index.php"); } else { $message = '<p> The username and password combination are incorrect.</p>'; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } else { $message .= '<p>Please try again.</p>'; } } if (isset($message)) { echo '<font color="red">', $message, '</font>'; } //$adminLogin = 'admin_login'; } //Closes function } //Closes class Connection Script (mysql_connect.php) // This file contains the database access information. This file also establishes a connection to MySQL and selects the database. // Set the database access information as constants. DEFINE ('DB_USER', 'atkinson'); DEFINE ('DB_PASSWORD', 'XYZ111WA'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'practicesite'); //include ('config.inc'); if ($dbc = @($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD))) { // Make the connnection. if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_NAME')))) { // If it can't select the database. // Handle the error. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not select the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // Print a message to the user, include the footer, and kill the script. echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. // Print a message to the user, include the footer, and kill the script. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not connect to the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of $dbc IF. // Function for escaping and trimming form data. function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysqli_real_escape_string( $dbc, trim ($data)); } // End of escape_data() function. Sometimes programming gets so frustrating! Please tell me I'm not a million miles away with this, otherwise I may just throw my laptop out of the office window! Thanks again to you both.
  16. Hi I am in the process of converting to Object Oriented from Procedural. To cater for this I have built an admin_login function, contained within a class: 'siteFunctions'. However, I am having trouble pointing the admin form to the function correctly. Every time I click 'submit', the form does not process anything. It doesn't even 'think' about it i.e. show the egg timer.... I have built this script heaps of times using the procedural method, so I guess I am somehow doing something wrong with respect to referencing the action attribute of the form (due to my new approach). I am very new to OO so please go easy on me: I know the script isn't particularly advanced. I just want to get used to putting functions into classes, and then calling the code, before I move onto more advanced stuff. I have placed all of the files within the same folder in order to rule out driectory path issues. Here are the three scripts that I think are relevant (login, functionsClass, and the mysql connection script): Login $pageTitle = "Admin Login"; include("admin_header.php"); include_once("sitefunctions.php"); new siteFunctions(); echo '<div class="admin_main_body">'; <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]);?>" method='post'> <input type="text" name="username" id="username" size="20"> <label>Username</label><br /> <input type="password" name="password" id="password" size="20"> <label>Password</label><br /> <input type="submit" name="submit" id="submit" value="submit"> </form> echo '<div>'; include("includes/admin_footer.php"); sitefunctions.php //$page = "admin_index.php"; class siteFunctions { var $message; function admin_login() { echo '<div class="admin_main_body">'; $message = NULL; if (isset($_POST['submit'])) { require_once ("mysql_connect.php"); if (empty($_POST['username'])) { $u = FALSE; $message .= '<p> Please enter your username </p>'; } else { $u = escape_data($_POST['username']); } if (empty($_POST['password'])) { $p = FALSE; $message .= '<p>You forgot to enter your password </p>'; } else { $p = escape_data($_POST['password']); } if ($u && $p) { // If everything's OK. $query = "SELECT * FROM admin WHERE username= ('$u') AND password=('$p')"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); $row = mysqli_fetch_array($result, MYSQLI_BOTH); if ($row) { session_start(); $_SESSION["admin_id"] = $row[0]; //header("$page"); //Redirects user to admin_index.php //header('location: "$page"'); header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "admin_index.php"); //echo '$_SESSION["admin_id"]'; } else { $message = '<p> The username and password combination are incorrect.</p>'; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } else { $message .= '<p>Please try again.</p>'; } } if (isset($message)) { echo '<font color="red">', $message, '</font>'; } //$adminLogin = 'admin_login'; } //Closes function } //Closes class Connection Script // This file contains the database access information. This file also establishes a connection to MySQL and selects the database. // Set the database access information as constants. DEFINE ('DB_USER', 'atkinson'); DEFINE ('DB_PASSWORD', 'XYZ111WA'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'practicesite'); if ($dbc = @($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD))) { // Make the connnection. if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_NAME')))) { // If it can't select the database. // Handle the error. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not select the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // Print a message to the user, include the footer, and kill the script. echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. // Print a message to the user, include the footer, and kill the script. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not connect to the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of $dbc IF. // Function for escaping and trimming form data. function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysqli_real_escape_string( $dbc, trim ($data)); } // End of escape_data() function. Any help would be appreciated. Cheers Will
  17. Hi I hope someone can help....although nobody else has been able to offer a solution that has worked, so I'm not holding my breath. I am very new to PHP, yet have been put in a position where I have to develop a web application, which uses an email and password login process. I need to build a script for a particular page that will allow the logged in user to view pieces of data, specific to them, from a MySQL table. Obviously, displaying all of the data in the table will not be appropriate. I need to just have the query run to pull a specific piece of data for display for the user. I have been over and over this script, and the other scripts within the application directory to see where I might be going wrong, but I just cannot find it. And it is getting to the stage, where I don't believe that I can figure it out!! Honestly - it is getting to the 'throw the damn machine through the window' point! Basically, my main areas of concern are that: 1/ I am misunderstanding the use of a session, to access and retrieve user data in a table. 2/ I am misunderstanding the scope of variables, and therefore calling on password/email verification methods within MySQL queries, that will not work, because I have not set up the code or set up the variables properly (or placed them in the right place). The aim of the script that I am trying to build is to retrieve a user 'balance' which is of floating point type, from the SQL table in order for it to be displayed on the user's 'profile page'. I have decided to post 2 scripts: login.php and account_balance.php, so that hopefully you can analyse and let me know what I am doing wrong and where my code needs correcting. The MySQL table structure is as follows: table name: at_client_profile column(attribute) order: client_id, password, trading_name, ind_type, balance, email I have been working on this script for a week or so, and have used numerous different approaches in my attempt to solve this. Currently I am receiving the following error message when I run the account_balance.php script: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Atkinsons\account_balance.php on line 17, which refers to the 'while($row = mysql_fetch_array($balance))' Here are the scripts: client_login.php , which I have included because it sets the variables that I am attempting to use in the SQL SELECT query, in order to retrieve specific user data. account_balance.php , which is where the SQL query is located... Start of Login script. // CHECK CLIENT LOGIN FORM if (isset($_POST['submit'])) { //handle the form. require_once ('inc/mysql_connect.php'); //Connect to the db. // Create a function for escaping the data. function escape_data ($data) { global $dbc; //Need the connection. if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysql_real_escape_string($data); } //End of function. $message = NULL; //Create an empty new variable. //Check for an email. if (empty($_POST['email'])) { $e = FALSE; $message .= '<p> You forgot to enter your email!</p>'; } else { $e = escape_data($_POST['email']); } //Check for a password. if (empty($_POST['password'])) { $p = FALSE; $message .= '<p>You forgot to enter your password!</p>'; } else { $p = escape_data($_POST['password']); } if ($e && $p) { // If email and password have been verified.... //Retrieve the client_id and trading_name for that username/password combination. $query = "SELECT * FROM at_client_profile WHERE email = ('$e') AND password = ('$p')"; $result = @mysql_query ($query); //Run the query. $row = mysql_fetch_array ($result, MYSQL_NUM); //Return a record, if applicable. if ($row) {//A record was pulled from the database. //Set the cookies/sessions & redirect. session_name ('clientlive'); //To set session name. session_start(); $_SESSION['client_id'] = $row[0]; $_SESSION['password'] = $p; $_SESSION['trading_name'] = $row[2]; $_SESSION['ind_type'] = $row[3]; $_SESSION['balance'] = $row[4]; $_SESSION['email'] = $e; header ("location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) ."./client_profile.php"); exit(); //Quit the script. } else { // No record matched the query. $message ='<p> The email and password entered do not match those on file.</p>'; } //mysql_close(); //Close the daabase connection. } else { $message .='<p> Please try again.</p>'; } End of Login Script. Start of account balance script. session_name ('clientlive'); session_start(); include ('inc/header.php'); include ('inc/left.php'); include ('inc/footer.php'); //Include the HTML footer. $query = "SELECT balance FROM at_client_profile WHERE email = ('$e') AND password = ('$p')"; //$query = "SELECT balance FROM at_client_profile WHERE client_id= ".$_SESSION['client_id'].";'" $result = @mysql_query($query); // You need to have this line to get the data from the db $balance = mysql_num_rows ($result); //Assigns balance from SQL query to $balance variable. // if ($balance > 0) { //If it ran OK, display the records. while($row = mysql_fetch_array($balance)) { echo "The balance of your account is".$row['balance']; echo "<br />"; } //echo "<p><big> <b> The outstanding balance on your account is $balance </b></big></p>"; //mysql_free_result ($result); //Free up the resources. //} else { // echo '<p> You do not have a balance outstanding. If you wish to enquire further please call a member of staff.</p>'; //If it did not run ok. //} mysql_close(); End of account balance script. Other points: The account_balance.php script is linked via another script, account_profile.php I know that this is a tall order, but as I can't figure it out, and I'm pretty new to PHP, I thought the best thing to do was look for some help... ??? Thanks
×
×
  • 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.