Jump to content

garyed

Members
  • Posts

    181
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

garyed's Achievements

Advanced Member

Advanced Member (4/5)

1

Reputation

  1. I'm learning things like this the hard way & I really appreciate all the constuctive help. The majority of my code is in php & the errors of my ways are building up.
  2. Thanks for the heads up. It looks like isNaN even returns false on an empty input.
  3. I think this is probably the best solution but I have about 100 different input fields to sort through & about 60 of them need to be numeric. I already had a list of the those 60 field names in an array so I thoght it would be a good idea to do it by including a simple external .js file & leaving the original file alone. I do like your idea & really appreciate the help. I'm learning as I go & didn't even know getElementsByClassName even existed.
  4. Thanks to all, I appreciate the ideas. If I'm understanding things correctly shouldn't the line in the above code be the letter "i" instead of "0" in the: let linker = item[0].value Here's what I came up with that seems to work: const arrayone= ["it_c","it_h","ot_c","ot_h"]; function hasNumericValue(arr) { // check for numeric values posted in the array of the input fields var items, comd1, comd2; for (let i = 0; i < arr.length; i++) { items=arr[i]; comd1="document.tester."+items+".value"; comd2=eval(comd1); if (isNaN(comd2)) { alert(comd2+ " is not a number"); return false; } } alert("all the numbers needed have been entered"); return true; }
  5. I have a lot of inputs on a form & a good many have to be numeric. I have all the names of the inputs that need to be posted as numbers in an array. I don't want to use type="number" on the inputs but would rather use a javascript function that would return either true or false when submitting the form with onsubmit= "return function()". This way I can put up an alert & display the field in error & it won't change the look of the page. The problem is I can't figure out how to use the elements of the array in the commands needed to test them. I can figure out how to check them for numeric values individually but at this point I just need to figure out how I can use the elements of the array in the command : documet.form_name.input_name.value so I can check them all. Here's a temporary code: <html> <head> </head> <body> <script> const arrayone= ["it_c","it_h","ot_c","ot_h."]; function hasNumericValue(arr) { // check for numeric values posted in the array of the input fields var linker; var items; for (let i = 0; i < arr.length; i++) { items=arr[i]; linker="document.tester."+items+".value"; alert("you made it to step2"+ linker) if (linker == 10) { alert("it works true"); return true; } } alert("it doesn't worlk False"); return false; } </script> <form name="tester" method="post" action="" onsubmit="return hasNumericValue(arrayone);" > <input name="it_c" type="text" value=""> <br> <input name="it_h" type="text" value=""> <br> <input name="ot_c" type="text" value=""> <br> <input name="ot_h" type="text" value=""> <br> <input type="submit" value="submit"> </form> </body> </html>
  6. I thought about marking this thread as solved but I would be lying because I still am in the process of trying to solve all my errors. It seems rather ridiculous that one version of php will work with errors turned off while a newer version won't. I've already fixed over a hundred errors in php 8.x & things are looking worse than before I started. I can go back to php 7.4 & the program will work flawlessly. If it's not an undefined variable error, it's a string & int error or something else. It's frustrating because I spent years writing this program & just learned as I was going along. It's been working fine for 10 years & now the upgrade to php 8.x has killed it. I've got over 1000 variables I'm dealing with & hundreds of calculations so it's not a simple task.
  7. I appreciate the help. It's a lot of good information that I've got to process. I'm a little slow at times but i'll eventually get it.
  8. I'm tried your code but it still doesn't insert the session data in the fileds & dropdown when I leave & return. I'm not sure what I'm missing but the only thing I know to do is to add something similar to what I already have to get it to work.
  9. Thanks, I'll check into "heredocs". I never even heard of it before you just mentioned it. I'm also going to study your code until I understand it better. What I'm really trying to do is to havel all 50 states in the dropdown menu from a MYSQL database & put whichever one is selected into the session. Then change the page to perform another task then return to the page & have the state from the session be already selected upon return.
  10. It seems to work fine but I would like to hear what you guys who are more versed in php think. In my previous post I was trying find a way to hide the errors but I really want to get things right this time. This is a sample code that I'm using as a test but on my site I'm going to be pulling my variables off a database & using a while loop to fill in all 50 states. <?php error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); // Starts session & uses function to keep data */ function get_value($var) { // this will check your session if it's not empty and you send an empty post if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { $_SESSION[$var]=$_POST[$var]; } if (isset($_SESSION[$var])){ return $_SESSION[$var];} else{ return $_POST[$var];} } if(!isset($_POST['submitter'])) {$_POST['submitter']="anything"; } // initialize to avoid Undefined array key error ?> <form name="test" action="" method="post"> <input type="text" style="width:255px;" name="owner" value="<?php if(isset($_POST['owner'])) {echo get_value('owner'); } else { if(isset($_SESSION['owner'])){ echo $_SESSION['owner'];} } ?>"> <br> <input type="text" style="width:255px;" name="renter" value="<?php if(isset($_POST['renter'])) {echo get_value('renter');}else { if(isset($_SESSION['renter'])){ echo $_SESSION['renter'];} } ?>"> <br> <select name="state" > <option value=""> select state </option> <?php $state1="Maine" ; $state2="Texas" ; ?> <option value="<?php echo $state1; ?>" <?php if(isset($_POST['state'])) { if(get_value('state') == $state1) { echo 'selected="selected"'; } } else { if(isset($_SESSION['state'])){ if ($_SESSION['state']==$state1) { echo 'selected="selected"'; } } } ?> > Maine </option> <option value="<?php echo $state2; ?>" <?php if(isset($_POST['state'])) {if (get_value('state')==$state2) { echo 'selected="selected"'; }} else { if(isset($_SESSION['state'])){ if ($_SESSION['state']==$state2) { echo 'selected="selected"'; } } } ?> > Texas </option> </select> <br> <br> <br> <input type="submit" value="submit"> &nbsp; &nbsp; <input name="submitter" type="submit" value="clear session"><br> </form> <?php if ($_POST['submitter']=="clear session") { echo $_POST['submitter']."<br>"; session_destroy(); }; print_r($_SESSION); ?>
  11. This seems like a hard way to do it but it looks like it works OK. Any suggestions ? value="<?php if(isset($_POST['owner'])) {echo get_value('owner'); } else { if(isset($_SESSION['owner'])){ echo $_SESSION['owner'];} } ?> "
  12. Thanks for the ideas, I can get rid of the errors by initiating $_POST['submitter'] & testing if $_POST['owner'] is initiated but then my session variable gets lost when leaving the page. Keeping the session variable is where my problem is. if(!isset($_POST['submitter'])) {$_POST['submitter']="hello"; } // insert before using $_POST['submitter'] // change value of input to this: value="<?php if(isset($_POST['owner'])) {echo get_value('owner'); }?> " The session variable is really not lost but it doesn't appear in the input field after leaving the page & coming back to it which is the problem especially if I'm dealing with a lot of inputs.
  13. Follow up with code: I didn't know if i should post this as a separate thread or not since the question is about fixing errors instead of covering them up. I'm trying to figure out how to get rid of the errors in this code because it's very similar to my site with a lot more inputs & I'm stumped. <!DOCTYPE html> <html> <head> </head> <body> <?php error_reporting(E_ALL); ini_set('display_errors', '1'); session_start(); // Starts session & uses function to keep data */ function get_value($var) { // this will check your session if it's not empty and you send an empty post if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { $_SESSION[$var]=$_POST[$var]; } if (isset($_SESSION[$var])){ return $_SESSION[$var];}else{ return $_POST[$var];} } ?> <form name="test" action="" method="post"> <input type="text" style="width:255px;" name="owner" value="<?php echo get_value('owner'); ?> "> <br> <input type="submit" value="submit"> &nbsp; &nbsp; <input name="submitter" type="submit" value="clear session"><br> </form> <?php if ($_POST['submitter']=="clear session") { session_destroy(); } print_r($_SESSION); ?> </body> </html>
  14. Thanks for the encouragement, I know I'll get it done but it's just going to take time. I just do this as a hobby & the code on the site in question I wrote about 10 years ago. When you don't do it everyday, it's easy to forget how you did something that far back. I was just hoping I could fool php8.0 like i did 7.2 for a little while longer
  15. I understand what you're saying & I definitely want to straighten the code out & get rid of all the errors properly. I had the same problem about a year ago,when my webhost upgraded the php version to 7.4. I turned off the errors to keep the site up & running & started trying to fix things but I just gave up after a couple days. I got lazy since everything was still working & just forgot about doing anything about the errors. I should have fixed things while I had the time but now I'm paying the price for procrastinating.
×
×
  • 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.