Jump to content

webguync

Members
  • Posts

    951
  • Joined

  • Last visited

Everything posted by webguync

  1. I have a form, that used JQuery for some effects and PHP to submit the form. It works fine, but I want to make some improvements. For one, the highlight class is added when you try and submit without filling in the fields which is great,but I would like that class to be removed when you click within a form field after it was added. Another improvement is I would like to add some text when you submit without filling in the fields and then subsequently remove when you click inside a field element. Something like "Please fill in the name field" etc. Here is my JQuery code. <script type="text/javascript"> /* <![CDATA[ */ var J = jQuery.noConflict(); J(document).ready(function() { //if submit button is clicked J('#submit').click(function () { //Get the data from all the fields var name = J('input[name=name]'); var email = J('input[name=email]'); var website = J('input[name=website]'); var comment = J('textarea[name=comment]'); //Simple validation to make sure user entered something //If error found, add highlight class to the text field if (name.val()=='') { name.addClass('highlight'); return false; } else {name.removeClass('highlight')}; if (email.val()=='') { email.addClass('highlight'); return false; } else {email.removeClass('highlight')}; if (website.val()=='') { website.addClass('highlight'); return false; } else {website.removeClass('highlight')}; if (comment.val()=='') { comment.addClass('highlight'); return false; } else {comment.removeClass('highlight')}; //organize the data properly var data = 'name=' + name.val() + '&email=' + email.val() + '&website=' + website.val() + '&comment=' + encodeURIComponent(comment.val()); //disabled all the text fields J('.text').attr('disabled','true'); //show the loading sign J('.loading').show(); //start the ajax J.ajax({ //this is the php file that processes the data and send mail url: "process.php", //GET method is used type: "GET", //pass the data data: data, //Do not cache the page cache: false, //success success: function (html) { //if process.php returned 1/true (send mail success) if (html==1) { //hide the form J('.form').fadeOut('slow'); //show the success message J('.done').fadeIn('slow'); //add height to element //if process.php returned 0/false (send mail failed) } else alert('Sorry, unexpected error. Please try again later.'); } }); //cancel the submit button default behaviours return false; }); }); /* ]]> */ </script> here is the php portion <?php //Retrieve form data. //GET - user submitted data using AJAX //POST - in case user does not support javascript, we'll use POST instead $name = ($_GET['name']) ? $_GET['name'] : $_POST['name']; $email = ($_GET['email']) ?$_GET['email'] : $_POST['email']; $website = ($_GET['website']) ?$_GET['website'] : $_POST['website']; $comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment']; //flag to indicate which method it uses. If POST set it to 1 if ($_POST) $post=1; //Simple server side validation for POST data, of course, you should validate the email if (!$name) $errors[count($errors)] = 'Please enter your name.'; if (!$email) $errors[count($errors)] = 'Please enter your email.'; if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; //if the errors array is empty, send the mail if (!$errors) { //recipient $to = 'Bruce Gilbert <webguync@gmail.com>'; //sender $from = $name . ' <' . $email . '>'; //subject and the html message $subject = 'Comment from ' . $name; $message = ' <!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></head> <body> <table> <tr><td>Name</td><td>' . $name . '</td></tr> <tr><td>Email</td><td>' . $email . '</td></tr> <tr><td>Website</td><td>' . $website . '</td></tr> <tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr> </table> </body> </html>'; //send the mail $result = sendmail($to, $subject, $message, $from); //if POST was used, display the message straight away if ($_POST) { if ($result) echo 'Thank you! I have received your message.'; else echo 'Sorry, unexpected error. Please try again later'; //else if GET was used, return the boolean value so that //ajax script can react accordingly //1 means success, 0 means failed } else { echo $result; } //if the errors array has values } else { //display the errors message for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>'; echo '<a href="index.php">Back</a>'; exit; } //Simple mail function with HTML header function sendmail($to, $subject, $message, $from) { $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: ' . $from . "\r\n"; $result = mail($to,$subject,$message,$headers); if ($result) {return 1;} else {return 0;} } ?> essentially I just need to remove the class highlight after it has been applied when a user clicks in a form field and also add some text indicating which fields were missed or no filled in properly.
  2. I am working on a project where I want a select form to display information from a MySQL table. The select values will be different sports (basketball,baseball,hockey,football) and the display will be various players from those sports. I have set up so far two tables in MySQL. One is called 'sports' and contains two columns. Once called 'category_id' and that is the primary key and auto increments. The other column is 'sports' and contains the various sports I mentioned. For my select menu I created the following code. <?php #connect to MySQL $conn = @mysql_connect( "localhost","uname","pw") or die( "You did not successfully connect to the DB!" ); #select the specified database $rs = @mysql_SELECT_DB ("test", $conn ) or die ( "Error connecting to the database test!"); ?> <html> <head>Display MySQL</head> <body> <form name="form2" id="form2"action="" > <select name="categoryID"> <?php $sql = "SELECT category_id, sport FROM sports ". "ORDER BY sport"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<option value=\"".$row['category_id']."\">".$row['sport']."</option>\n "; } ?> </select> </form> </body> </html> this works great. I also created another table called 'players' which contains the fields 'player_id' which is the primary key and auto increments, category_id' which is the foreign key for the sports table, sport, first_name, last_name. The code I am using the query and display the desired result is as follows <html> <head> <title>Get MySQL Data</title> </head> <body> <?php #connect to MySQL $conn = @mysql_connect( "localhost","uname","pw") or die( "Err:Db" ); #select the specified database $rs = @mysql_SELECT_DB ("test", $conn ) or die ( "Err:Db"); #create the query $sql ="SELECT * FROM sports INNER JOIN players ON sports.category_id = players.category_id WHERE players.sport = 'Basketball'"; #execute the query $rs = mysql_query($sql,$conn); #write the data while( $row = mysql_fetch_array( $rs) ) { echo ("<table border='1'><tr><td>"); echo ("Caetegory ID: " . $row["category_id"] ); echo ("</td>"); echo ("<td>"); echo ( "Sport: " .$row["sport"]); echo ("</td>"); echo ("<td>"); echo ( "first_name: " .$row["first_name"]); echo ("</td>"); echo ("<td>"); echo ( "last_name: " .$row["last_name"]); echo ("</td>"); echo ("</tr></table>"); } ?> </body> </html> this also works fine. All I need to do is tie the two together so that when a particular sport is selected, the query will display below in a table. I know I need to change my WHERE clause to a variable. This is what I need help with. thanks
  3. I am trying to learn about sorting and modifying arrays and I am practicing on a book assignment but need some help to grasp what I need to do. If you go here, you will see how it is supposed to work. http://198.86.244.3/mabarckhoff/WEB182/ch11_ex1/ I need to write a case to sort the task when you click the sort button and modify when you click that button. The delete button works and the add task works. I understand the sort array and have added that case, but don't know how to get it to work with the submit. Also there is a promote button so would need 'promote' what ever you have chosen in the dropdown menu to the top. Here is my code. tasklist.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> <title>Task List Manager</title> <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body> <div id="page"> <div id="header"> <h1>Task List Manager</h1> </div> <div id="main"> <!-- part 1: the errors --> <?php if (count($errors) > 0) : ?> <h2>Errors:</h2> <ul> <?php foreach($errors as $error) : ?> <li><?php echo $error; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> <!-- part 2: the tasks --> <h2>Tasks:</h2> <?php if (count($task_list) == 0) : ?> <p>There are no tasks in the task list.</p> <?php else: ?> <ul> <?php foreach( $task_list as $id => $task ) : ?> <li><?php echo $id + 1 . '. ' . $task; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> <br /> <!-- part 3: the add form --> <h2>Add Task:</h2> <form action="." method="post" > <?php foreach( $task_list as $task ) : ?> <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/> <?php endforeach; ?> <label>Task:</label> <input type="text" name="newtask" id="newtask"/> <br /> <label> </label> <input type="submit" name="action" value="Add Task"/> </form> <br /> <!-- part 4: the modify/promote/delete form --> <?php if (count($task_list) > 0 && empty($task_to_modify)) : ?> <h2>Select Task:</h2> <form action="." method="post" > <?php foreach( $task_list as $task ) : ?> <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/> <?php endforeach; ?> <label>Task:</label> <select name="taskid"> <?php foreach( $task_list as $id => $task ) : ?> <option value="<?php echo $id; ?>"> <?php echo $task; ?> </option> <?php endforeach; ?> </select> <br /> <label> </label> <input type="submit" name="action" value="Modify Task"/> <input type="submit" name="action" value="Promote Task"/> <input type="submit" name="action" value="Delete Task"/> <br /> <label> </label> <input type="submit" name="action" value="Sort Tasks"/> </form> <?php endif; ?> <!-- part 5: the modify save/cancel form --> <?php if (!empty($task_to_modify)) : ?> <h2>Task to Modify:</h2> <form action="." method="post" > <?php foreach( $task_list as $task ) : ?> <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/> <?php endforeach; ?> <label>Task:</label> <input type="hidden" name="modifiedtaskid" value="<?php echo $task_index; ?>" /> <input type="text" name="modifiedtask" value="<?php echo $task_to_modify; ?>" /><br /> <label> </label> <input type="submit" name="action" value="Save Changes"/> <input type="submit" name="action" value="Cancel Changes"/> </form> <?php print_r($_POST); ?> <?php endif; ?> </div><!-- end main --> </div><!-- end page --> </body> </html> index.php <?php if (isset($_POST['tasklist'])) { $task_list = $_POST['tasklist']; } else { $task_list = array(); // some hard-coded starting values to make testing easier $task_list[] = 'Write chapter'; $task_list[] = 'Edit chapter'; $task_list[] = 'Proofread chapter'; } $errors = array(); switch( $_POST['action'] ) { case 'Add Task': $new_task = $_POST['newtask']; if (empty($new_task)) { $errors[] = 'The new task cannot be empty.'; } else { $task_list[] = $new_task; } break; case 'Delete Task': $task_index = $_POST['taskid']; unset($task_list[$task_index]); $task_list = array_values($task_list); break; case 'Sort Task': sort($task_list); print_r($task_list); break; /* case 'Save Changes': case 'Cancel Changes': case 'Promote Task': */ } include('task_list.php'); ?>
  4. I figured out the problem to clear the form. Marking resolved. Thanks for the help!
  5. I think this is working pretty much the way I want. The only slight problem is the reset form using JS only works prior to hitting the submit. Once the POST data is submitted. You cannot clear the form. Any suggestions for that. current code: <?php if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { $err = array(); if( empty($_POST['name']) ) $err['name'] = 'Name is a required field'; elseif( !preg_match('/^[a-z]++$/i', $_POST['name']) ) $err['name'] = 'Name can only be letters'; if( empty($_POST['email']) ) $err['email'] = 'Email is a required field'; elseif( !preg_match('/.+@.+\..+/', $_POST['email']) ) $err['email'] = 'Email must contain @ symbol'; if( empty($_POST['phone']) ) $err['phone'] = 'Phone is a required field'; elseif( !preg_match('/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i', $_POST['phone']) ) $err['phone'] = 'Phone number is not entered in a valid format [123-123-1234]'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>String Tester</title> <link rel="stylesheet" type="text/css" href="main.css"/> <script type="text/javascript"> function clearForm(oForm) { var elements = oForm.elements; oForm.reset(); for(i=0; i<elements.length; i++) { field_type = elements[i].type.toLowerCase(); switch(field_type) { case "text": case "password": case "textarea": case "hidden": elements[i].value = ""; break; case "radio": case "checkbox": if (elements[i].checked) { elements[i].checked = false; } break; case "select-one": case "select-multi": elements[i].selectedIndex = -1; break; default: break; } } } </script> </head> <body onLoad="clearForms()" onUnload="clearForms()"> <div id="content"> <h1>String Tester</h1> <form method="post" action="<?php echo $_SERVER['index_test.php']; ?>"> <label>Name: <input type="text" name="name" value="<?php if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>"></label> <?php if(isset($err['name'])) echo '<span style="color: red;">'.$err['name'].'</span>' ?> <br> <label>Email: <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>"></label> <?php if(isset($err['email'])) echo '<span style="color: red;">'.$err['email'].'</span>' ?> <br> <label>Phone: <input type="text" name="phone" value="<?php if(isset($_POST['phone'])) echo htmlspecialchars($_POST['phone']); ?>"></label> <?php if(isset($err['phone'])) echo '<span style="color: red;">'.$err['phone'].'</span>' ?> <br> <input type="submit" value="submit" /> <br><br> <input type="button" name="reset_form" value="Reset Form" onClick="this.form.reset();"> </form> <?php if( empty($err) ) { //function_to_process_data(); echo "<h2>Message:</h2>"; echo "<table>"; echo "<tr><td>"; echo ucwords($_POST['name']); echo "</td></tr>"; echo "<tr><td>"; echo $_POST['email']; echo "</td></tr>"; echo "<tr><td>"; echo $_POST['phone']; echo "</td></tr>"; echo "</table>"; exit(); } ?> </div> </body> </html>
  6. ok thanks. That solved the problem.
  7. never mind, I wasn't echoing the post data. I would like the form to still display though and the echoed data to just display below the form. <?php if( $_SERVER['REQUEST_METHOD'] == 'POST' ) { $err = array(); if( empty($_POST['name']) ) $err['name'] = 'Name is a required field'; elseif( !preg_match('/^[a-z]++$/i', $_POST['name']) ) $err['name'] = 'Name can only be letters'; if( empty($_POST['email']) ) $err['email'] = 'Email is a required field'; elseif( !preg_match('/@/', $_POST['email']) ) $err['email'] = 'Email must contain @ symbol'; if( empty($err) ) { //function_to_process_data(); echo "<h2>Message:</h2>"; echo "<table>"; echo "<tr><td>"; echo $_POST['name']; echo "</td></tr>"; echo "<tr><td>"; echo $_POST['email']; echo "</td></tr>"; echo "<tr><td>"; echo $_POST['phone']; echo "</td></tr>"; echo "</table>"; exit(); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>String Tester</title> <link rel="stylesheet" type="text/css" href="main.css"/> <script type="text/javascript"> function clearForm(oForm) { var elements = oForm.elements; oForm.reset(); for(i=0; i<elements.length; i++) { field_type = elements[i].type.toLowerCase(); switch(field_type) { case "text": case "password": case "textarea": case "hidden": elements[i].value = ""; break; case "radio": case "checkbox": if (elements[i].checked) { elements[i].checked = false; } break; case "select-one": case "select-multi": elements[i].selectedIndex = -1; break; default: break; } } } </script> </head> <body onLoad="clearForms()" onUnload="clearForms()"> <div id="content"> <h1>String Tester</h1> <form method="post" action="<?php echo $_SERVER['index_test.php']; ?>"> <label>Name: <input type="text" name="name" value="<?php if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>"></label> <?php if(isset($err['name'])) echo '<span style="color: red;">'.$err['name'].'</span>' ?> <br> <label>Email: <input type="text" name="email" value="<?php if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>"></label> <?php if(isset($err['email'])) echo '<span style="color: red;">'.$err['email'].'</span>' ?> <br> <input type="submit" value="submit" /></td> <br> <input type="button" name="reset_form" value="Reset Form" onClick="this.form.reset();"> </form> </div> </body> </html>
  8. I need to add ucwords so that the first letter is caps in POST data. Not sure where to put in within this. echo $_POST['name'];
  9. thanks. also I would want the results to echo out below the form if there are no errors. Where would I do that with your code? I don't really need to clear the page after the submit.
  10. ok so basicly if this script is contained on the index.php this part would be: <?php echo $_SERVER['index.php']; ?>
  11. I was kind of hoping to just use the php I have set up for that. This is going to be live, so I don't need to do anything fancy. also I added <input type="reset" name="reset" value="reset"> but that doesn't reset the form, maybe because it is using PHP post variables?
  12. ok thanks, I fixed that part. Now how can I debug why the error msg isn't displaying below the form field when it is empty or not entered correctly? I should have the check set up for that but the error msg. doesn't display, only the class to highlight the field. Also what is the best way to reset the form? I want it to clear when a refresh is done. thanks!
  13. here is an update. I consolidated my code and it works to an extent. The variable result are echoes on the page. However, the message invalid phone number is displayed no matter what and the error messages for name and email don't display, however the input field is highlighted in red as intended. <?php // If request is a form submission if($_SERVER['REQUEST_METHOD'] == 'POST'){ // Validation // Check first_name is non-blank if(0 === preg_match("/\S+/", $_POST['name'])){ $errors['name'] = "Please enter a name."; } // Check email is valid (enough) if(0 === preg_match("/.+@.+\..+/", $_POST['email'])){ $errors['email'] = "Please enter a valid email address."; } // validate a phone number if( !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) { echo 'Please enter a valid phone number'; } // If no validation errors if(0 === count($errors)){ // Sanitize first, last, and email $name = ($_POST['name']); $email= ($_POST['email']); $phone= ($_POST['phone']); } } require('functions.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> <title>String Tester</title> <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body> <div id="content"> <h1>String Tester</h1> <form action="index.php" method="post"> <table> <tr class="<?php echo form_row_class("name") ?>" > <th><label for="name">Name:</label></th> <td> <input name="name" id="name" type="text" value="<?php echo h($_POST['name']); ?>" /> <?php echo error_for('name') ?> </td> </tr> <tr class="<?php echo form_row_class("email") ?>"> <th><label for="email">Email Address:</label></th> <td> <input name="email" id="email" type="text" value="<?php echo h($_POST['email']); ?>" /> <?php echo error_for('email') ?> </td> </tr> <tr class="<?php echo form_row_class("phone") ?>"> <th><label for="phone">Phone:</label></th> <td> <input name="phone" id="phone" type="text" value="<?php echo h($_POST['phone']); ?>" /> <?php echo error_for('phone') ?> </td> </tr> <tr> <th></th> <td><input type="submit" value="submit" /></td> </tr> </table> </form> <h2>Message:</h2> <?php echo "<table>"; echo "<tr><td>"; echo $name; echo "</td></tr>"; echo "<tr><td>"; echo $email; echo "</td></tr>"; echo "<tr><td>"; echo $phone; echo "</td></tr>"; echo "</table>"; ?> </div> </body> </html> functions <?php // Functions function form_row_class($name){ global $errors; return $errors[$name] ? "form_error_row" : ""; } function error_for($name){ global $errors; } if($errors[$name]){ return "<div class='form_error'>" . $errors[$name] . "</div>"; } function h($string){ return htmlspecialchars($string); } ?>
  14. ok, thx. I fixed that part. The form isn't displaying the submit results though. It echoes an invalid phone number when I enter it as 123-123-1234 format. here is my updated code: index.php <?php require('functions.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> <title>String Tester</title> <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body> <div id="content"> <h1>String Tester</h1> <form action="submit.php" method="post"> <table> <tr class="<?php echo form_row_class("name") ?>" > <th><label for="name">Name:</label></th> <td> <input name="name" id="name" type="text" value="<?php echo h($_POST['name']); ?>" /> <?php echo error_for('name') ?> </td> </tr> <tr class="<?php echo form_row_class("email") ?>"> <th><label for="email">Email Address:</label></th> <td> <input name="email" id="email" type="text" value="<?php echo h($_POST['email']); ?>" /> <?php echo error_for('email') ?> </td> </tr> <tr class="<?php echo form_row_class("phone") ?>"> <th><label for="phone">Phone:</label></th> <td> <input name="phone" id="phone" type="text" value="<?php echo h($_POST['phone']); ?>" /> <?php echo error_for('phone') ?> </td> </tr> <tr> <th></th> <td><input type="submit" value="submit" /></td> </tr> </table> </form> <h2>Message:</h2> <?php echo "<table>"; echo "<tr><td>"; echo $name; echo "</td></tr>"; echo "<tr><td>"; echo $email; echo "</td></tr>"; echo "<tr><td>"; echo $phone; echo "</td></tr>"; echo "</table>"; ?> </div> </body> </html> submit.php <?php // If request is a form submission if($_SERVER['REQUEST_METHOD'] == 'POST'){ // Validation // Check first_name is non-blank if(0 === preg_match("/\S+/", $_POST['name'])){ $errors['name'] = "Please enter a name."; } // Check email is valid (enough) if(0 === preg_match("/.+@.+\..+/", $_POST['email'])){ $errors['email'] = "Please enter a valid email address."; } // validate a phone number if( !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) { echo 'Please enter a valid phone number'; } // If no validation errors if(0 === count($errors)){ // Sanitize first, last, and email $name = ($_POST['name']); $email= ($_POST['email']); $phone= ($_POST['phone']); } } require('functions.php'); ?> functions.php <?php // Functions function form_row_class($name){ global $errors; return $errors[$name] ? "form_error_row" : ""; } function error_for($name){ global $errors; } if($errors[$name]){ return "<div class='form_error'>" . $errors[$name] . "</div>"; } function h($string){ return htmlspecialchars($string); } ?>
  15. would this be right now? <?php // Functions function form_row_class($name){ global $errors; return $errors[$name] ? "form_error_row" : ""; } function error_for($name){ global $errors; } if($errors[$name]){ return "<div class='form_error'>" . $errors[$name] . "</div>"; } function h($string){ return htmlspecialchars($string); } ?> also when I click submit I get an error Parse error: syntax error, unexpected $end in /nfs/c08/h01/mnt/118256/domains/inspired-evolution.com/html/Testing/Strings/ch09_ex1/submit.php on line 40 here is the code for submit.php <?php // If request is a form submission if($_SERVER['REQUEST_METHOD'] == 'POST'){ // Validation // Check first_name is non-blank if(0 === preg_match("/\S+/", $_POST['name'])){ $errors['name'] = "Please enter a name."; } // Check email is valid (enough) if(0 === preg_match("/.+@.+\..+/", $_POST['email'])){ $errors['email'] = "Please enter a valid email address."; } // validate a phone number if( !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) { echo 'Please enter a valid phone number'; } // If no validation errors if(0 === count($errors)){ // Sanitize first, last, and email $name = mysql_real_escape_string($_POST['name']); $email= mysql_real_escape_string($_POST['email']); $email= mysql_real_escape_string($_POST['phone']); require('functions.php'); ?>
  16. I put the functions in an include but now I am getting the following error. "Parse error: syntax error, unexpected $end in functions.php on line 18". the code to include the functions is: <?php ini_set('display_errors',1); error_reporting(-1); require('functions.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> <title>String Tester</title> <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body> <div id="content"> <h1>String Tester</h1> <form action="submit.php" method="post"> <table> <tr class="<?php echo form_row_class("name") ?>" > <th><label for="first_name">Name</label></th> <td> <input name="first_name" id="first_name" type="text" value="<?php echo h($_POST['name']); ?>" /> <?php echo error_for('name') ?> </td> </tr> <tr class="<?php echo form_row_class("email") ?>"> <th><label for="email">Email Address</label></th> <td> <input name="email" id="email" type="text" value="<?php echo h($_POST['email']); ?>" /> <?php echo error_for('email') ?> </td> </tr> <tr> <th></th> <td><input type="submit" value="submit" /></td> </tr> </table> </form> <h2>Message:</h2> <?php echo "<table>"; echo "<tr><td>"; echo $name; echo "</td></tr>"; echo "<tr><td>"; echo $email; echo "</td></tr>"; echo "<tr><td>"; echo $phone; echo "</td></tr>"; echo "</table>"; ?> </div> </body> </html> and functions.php is: <?php // Functions function form_row_class($name){ global $errors; return $errors[$name] ? "form_error_row" : ""; } function error_for($name){ global $errors; if($errors[$name]){ return "<div class='form_error'>" . $errors[$name] . "</div>"; } function h($string){ return htmlspecialchars($string); } ?>
  17. I need to echo out some string variables in a form. I also want to add some validation for Name, email etc. I think I have the code right or close, but I can an error on the index.php page which is. "Fatal error: Call to undefined function form_row_class() in index.php on line 14" here is the code for the form <!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> <title>String Tester</title> <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body> <div id="content"> <h1>String Tester</h1> <form action="submit.php" method="post"> <table> <tr class="<?php echo form_row_class("name") ?>" > <th><label for="first_name">Name</label></th> <td> <input name="first_name" id="first_name" type="text" value="<?php echo h($_POST['name']); ?>" /> <?php echo error_for('name') ?> </td> </tr> <tr class="<?php echo form_row_class("email") ?>"> <th><label for="email">Email Address</label></th> <td> <input name="email" id="email" type="text" value="<?php echo h($_POST['email']); ?>" /> <?php echo error_for('email') ?> </td> </tr> <tr> <th></th> <td><input type="submit" value="submit" /></td> </tr> </table> </form> <h2>Message:</h2> <?php echo "<table>"; echo "<tr><td>"; echo $name; echo "</td></tr>"; echo "<tr><td>"; echo $email; echo "</td></tr>"; echo "<tr><td>"; echo $phone; echo "</td></tr>"; echo "</table>"; ?> </div> </body> </html> and the submit portion <?php // If request is a form submission if($_SERVER['REQUEST_METHOD'] == 'POST'){ // Validation // Check first_name is non-blank if(0 === preg_match("/\S+/", $_POST['name'])){ $errors['name'] = "Please enter a name."; } // Check email is valid (enough) if(0 === preg_match("/.+@.+\..+/", $_POST['email'])){ $errors['email'] = "Please enter a valid email address."; } // validate a phone number if( !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) { echo 'Please enter a valid phone number'; } // If no validation errors if(0 === count($errors)){ // Sanitize first, last, and email $name = mysql_real_escape_string($_POST['name']); $email= mysql_real_escape_string($_POST['email']); $email= mysql_real_escape_string($_POST['phone']); } // Helpers function form_row_class($name){ global $errors; return $errors[$name] ? "form_error_row" : ""; } function error_for($name){ global $errors; if($errors[$name]){ return "<div class='form_error'>" . $errors[$name] . "</div>"; } function h($string){ return htmlspecialchars($string); } ?> probably something simple I am missing.
  18. In this form, I am using radio buttons to select various PHP math function results (total, average,both) and it works but was wondering if it is possible to make it multiple choice, that is to say instead of displaying one result at t time when you submit, displaying two or three of the results, depending on how many radio buttons are clicked. Can this be done? Here is the form code <form action="." method="post"> <input type="hidden" name="action" value="process_scores" /> <label>Score 1:</label> <input type="text" name="scores[]" value="<?php echo $scores[0]; ?>"/><br /> <label>Score 2:</label> <input type="text" name="scores[]" value="<?php echo $scores[1]; ?>"/><br /> <label>Score 3:</label> <input type="text" name="scores[]" value="<?php echo $scores[2]; ?>"/><br /> <!-- ADD LOGIC TO DETERMINE WHETHER TO CALCULATE AVERAGE, TOTAL OR BOTH --> <fieldset> <legend> What do you want to calculate?</legend> <p> <input type="radio" name="calculate" value="average" checked="checked" /> Average<br /> <input type="radio" name="calculate" value="total" />Total<br /> <input type="radio" name="calculate" value="both" />Both</p> <p><br /> </p> </fieldset> <br /><br /> <label> </label> <input type="submit" value="Process Scores" /><br /> <label>Scores:</label> <span><?php if (isset($scores_string)) { echo $scores_string; } ?></span><br /> <label>Score Total:</label> <span><?php if (isset($score_total)) { echo $score_total; } ?></span><br /> <label>Average Score:</label> <span><?php if (isset($score_average)) { echo $score_average; } ?></span><br /> </form> and the processing code <?php if (isset($_POST['action'])) { $action = $_POST['action']; } else { $action = 'start_app'; } if (isset($_POST['calculate'])) { $calculate = $_POST['calculate']; } switch ($action) { case 'start_app': $scores = array(); $scores[0] = 70; $scores[1] = 80; $scores[2] = 90; break; case 'process_scores': $scores = $_POST['scores']; // validate the scores $is_valid = true; for ($i = 0; $i < count($scores); $i++) { if (empty($scores[$i]) || !is_numeric($scores[$i])) { $scores_string = 'You must enter three valid numbers for scores.'; $is_valid = false; break; } } if (!$is_valid) { break; } // process the scores $scores_string = ''; $score_total = 0; foreach ($scores as $s) { $scores_string .= $s . '|'; $score_total += $s; } $scores_string = substr($scores_string, 0, strlen($scores_string)-1); // calculate the average $score_average = $score_total / count($scores); // format the total and average switch($calculate) { case 'average': $score_average = number_format($score_average, 2); $score_total = ""; break; case 'total': $score_average = ""; $score_total = number_format($score_total, 2); break; case 'both': $score_total = number_format($score_total, 2); $score_average = number_format($score_average, 2); break; } break; } include 'loop_tester.php'; ?>
  19. I appreciate the response, bu you know what? I tried that and still get access denied. And yes, I did make sure that I have root access enabled.
  20. don't know if this helps, but the server is hosted with Media Temple and it's a dedicated (DV) server. Here are the instructions they provide. http://kb.mediatemple.net/questions/16/Connecting+via+SSH+to+your+server#dv this is why i was doing the ssh root@70.32.86.175 deal. I was following their instructions. I even called them to make sure I was putting in the correct info and they said I was.
  21. I tried connecting using just the domain name when I get prompted the login and that doesn't grant me access either.
  22. sure, I open up PUTTY.EXE. for the host name I put in the host domain (I also tried IP address). The Port is 22 and the connection type is SSH. Per the instruction from my hosting provider for the login I should put in ssh root@serveripadress.com since it is a dedicated server. I get an access denied message when I do this. I verified with the tech support of the hosting provider that what I am doing is correct.
×
×
  • 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.