DavidAbineri Posted December 23, 2021 Share Posted December 23, 2021 I am attempting to load one of two pages based on a value entered into a text box (it will be a number). Here is what does not work and I am looking for some help in correcting this. $x and $y and $number represent given numbers. Thank you. <tr> <td> Enter your number </td><td><input type="text" name="number" id="max" value="<?php echo $number ?>" maxlength="10" size="10" style="background-color:#ffffa0"></td> </table> <p id="max"></p> <script type="text/javascript"> function OnSubmitForm() { var larger = document.getElementById("max").value; document.getElementById("demo").innerHTML =number; if (val(number)==val($x) ) { document.form.action="form1.php"; } elseif (val(number)==val($y) ) { form.action="form2.php" ; } return true; } </script> <table align= "center"><tr><td> <button input name="Submit" type="submit" class="buttonlightgreen" onclick="OnSubmitForm()" >Click to SUBMIT ABOVE DATA </button> </td></tr></table> </form> Quote Link to comment Share on other sites More sharing options...
Barand Posted December 23, 2021 Share Posted December 23, 2021 number has not been assigned a value in that code $x and $y are php variables and will not be available when the javascript runs. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 23, 2021 Share Posted December 23, 2021 (edited) 1 - once again - please use the code tags to post code 2 - you have mixed in the js code with php code and html code. Gotta change those habits. 3 - this cannot possibly run for you since your references to unknown as of yet variables won't show anything in the js code. 4 - you should not be using php vars inside js code. That is all I can say since even trying to organize your code into something that I could make sense of (very incomplete!) gave me heartache. Please - if you want us to look at why your code is not performing as you expect show us what you are looking at. Plus - look at the proper syntax for a <button> tag. You are not using it. Personally I like to use <input> tags for my submits, not the <button> one. Your use of the word 'input' inside a <button> tag makes no sense. The way to solve your dilemma is (I'm guessing here) to let this form handle sending out the selection screen and responding to it and getting whatever number was input and using that to jump to the page you want instead of using JS to do it. Edited December 23, 2021 by ginerjm Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 24, 2021 Author Share Posted December 24, 2021 I am clearly a newbie here but trying my best to understand all the comments that have been made. Here is what I am trying to do and UNDERSTAND. I am trying to load one of two forms (web pages) depending on the contents of a particular text box on a page already loaded. So, if there is a "1" the text box then form1 should load when the submit button is clicked and if there is a "2" in the text box then form2 should load. Perhaps you might give me suggestions as to where to begin researching the question. Can this be done in just html? Should I be using PHP? Should I be using JAVA? Some basic guidance would go along way to getting me started in understanding this issue. Thank you for guidance on this question. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 24, 2021 Share Posted December 24, 2021 (edited) Yes - it is quite simple. Correct your tags as I mentioned and use this form to ask the user to provide a number. Tell the form tag to come back to this script. Have this script check if it was properly submitted (check for method = post or the button name was proper) get the number from the form elements. If that value is correct then pass it through to the if statement that decides what form to jump to. Take that answer and do the following: header('Location: '.$url); exit(); where $url is the string you assemble in this script to go to the page you want to display. Note the exit() statement. It is recommended to use one here. HTH! Edited December 24, 2021 by ginerjm Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 24, 2021 Author Share Posted December 24, 2021 JG, Thank you for your hints. I have not used the <code> tag previously, what is the advantage of using this tag? Why use it? What is wrong with my tags? What do you mean by "Tell the form tag to come back to this script. "? How is this done? Sorry for all the questions, still on the steep part of the learning curve. David Abineri Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 24, 2021 Share Posted December 24, 2021 (edited) The code tag that I mention is used ON THIS PAGE when you post code for us to read. Makes it easier to focus on the written code that you are showing us. It looks like this: <> . Copy and paste your code sample that you want to show us and then click on the <> icon here and in the window that opens up you can paste your sample and then click on the 'insert in post' and it then shows up here in a highlighted box and then you can write your own text to ask about it or whatever.. If you look at other posts/topics I'm sure you will see how it is being used by everyone. As for the question you are trying to handle. My suggestion was to make this current form a simple one that only want the user to give you this response you are seeking - the number or perhaps better, a choice of one text label or another or maybe a couple of checkboxes and a submit button. Then have this current script read what was submitted and from that do this: if ($_SERVER['REQUEST_METHOD'] == 'POST') { $formname = ''; if (isset($_POST['checkbox1'])) $formname = 'form1.php'; if (isset($_POST['checkbox2'])) $formname = 'form2.php'; } if ($formname <> '') { header("Location: $formname"); exit(); } else { header("Location: thisform.php"); // redisplay this form exit(); } What this code does is process the input and call the next form that you want to execute. If the user did not check either box then it redisplays this page for them to do it again. You'll need to add some code to present an error message perhaps to tell the user to check one of the boxes. BTW - do you have error checking turned on to show you simple syntax errors and such? error_reporting(E_ALL); ini_set('display_errors', '1'); Add this to to the start of all of your scripts. Once you have debugged it all and it is running fine you can remove it or change the ini_set to a 0. PS - This post used the <> icon twice to present the two blocks of code I showed you. See how nice it appears? PPS - Be SURE that you copy&paste your sample code and do NOT re-type it here. Time and again something gets re-typed incorrectly and eventually the user admits that he/she has made such a mistake and we will have wasted our time solving the wrong issue. Edited December 24, 2021 by ginerjm Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 24, 2021 Share Posted December 24, 2021 Upon further review - you might wanna use radio buttons instead of checkboxes. Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 24, 2021 Author Share Posted December 24, 2021 Thanks, JG, I will play with these ideas! Looks to be very useful information, thanks for all the suggestions. David Abineri Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 24, 2021 Share Posted December 24, 2021 Feeling creative so here is a sample of how I would do the entry form: echo "<div id='form_box' style='width:40%;border:2px solid blue;padding:10px;'>"; // suggestion: learn how to use CSS and move the above styling to that area echo " <form method='POST' action=''> Choose which form you want to see by clicking on a button. <br><br> <label>Show Form #1 <input type='radio' name='form_sel' value='1'> </label> <label>Show Form #2 <input type='radio' name='form_sel' value='2'> </label> <br><br> <center> <input type='submit' name='btn' value='Show Form'> <input type='submit' name='btn' value='Return to Caller'> </center> </form> "; echo "</div>"; Now the input choice is named 'form_sel' so the previous php code needs this change: $formname = ''; if ($_POST['form_sel'] == '1') $formname = 'form1.php'; if ($_POST['form_sel'] == '2') $formname = 'form2.php'; Replace the part that checks for checkbox1 and checkbox2 with the above. This makes sure that the user can only make one selection whereas the use of check boxes would not. Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 25, 2021 Author Share Posted December 25, 2021 JG, It looks like you are using radio button here, not check boxes so does this only permit one of the two choices to be made? So choosing both is not possible here is it? David Abineri Quote Link to comment Share on other sites More sharing options...
dodgeitorelse3 Posted December 25, 2021 Share Posted December 25, 2021 You can read up on radio buttons at https://html5-tutorial.net/forms/radiobuttons/ Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 25, 2021 Share Posted December 25, 2021 Yes - 2 posts ago I suggested switching to radios and then I posted the code to change to that. I realized that problem too late. Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 25, 2021 Author Share Posted December 25, 2021 JG, I believe I am following what you have said so far. However , I need to make the choice of the form to be loaded based on the contents of a text box, not based on radio buttons or checkboxes. I just can't seem to make this happen yet. Thanks again, David Abineri Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 25, 2021 Share Posted December 25, 2021 (edited) I gave you all of the code you need to do that. Are you not reading it? Edited December 25, 2021 by ginerjm Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 25, 2021 Share Posted December 25, 2021 To be clear. I gave you a set of html code that is the form your user will see and make his choice from. Look at it an understand it. Then look at the previous PHP code that shows you how to handle the input from the user and that then calls the selected script. Be sure to include the part I revised from checkboxes to radio buttons. If you want I can give you an entire script that does this, but I figured you'd want to use these pieces in your own script. But... Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 26, 2021 Author Share Posted December 26, 2021 OK, here is a question that I cannot find an answer to. When using the POST method, it seems that only data that was entered by the user is sent to the called page. How do I also include in the POST, variables that I have used on that page that were not entered by the user? Thanks for all the help. David Abineri Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 26, 2021 Share Posted December 26, 2021 What kind of variables? You haven't mentioned anything like that at all so you are confusing me. If you have something that you created in the script that is sending this form to the user you could hide those values in 'hidden' input tags that would then be part of the POST array. echo " <form> --- --- ---- <input type='hidden' name='valuea' value='$valuea'> --- --- </form> "; If you added an input tag to your form tag you could assign a php variable to it as a value= with a name that you could then retrieve it from POST with. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 26, 2021 Share Posted December 26, 2021 Ok - it appears that you are really new to this since it takes you a long time to read and respond to my posts. That's ok but I thought I would give you a full picture tutorial that might help you see how it could be done. My coding style may have its critics but it is not totally 'bad' and does work well for what you seem to desire to accomplish. Study it please (more than the posts you have already glossed over) and learn from it. (clicking on the <> icon Now!) <?php /* * form_sel.php - allow the user to select which * page to display next * */ // turn on/off error reporting error_reporting(E_ALL); ini_set('display_errors', '1'); // 1 = on; 0 = off; $errmsg = ''; // // See if the form has been submitted yet // if ($_SERVER['REQUEST_METHOD'] == 'POST') { $formname = ''; if (isset($_POST['form_sel'])) { if ($_POST['form_sel'] == '1') $formname = 'form1.php'; if ($_POST['form_sel'] == '2') $formname = 'form2.php'; if ($formname <> '') { // The next line will jump to the selected page // uncomment when ready // header("Location: $formname"); // for testing this out // remove next 2 lines when done testing $errmsg = "You have chosen to see page $formname"; DisplayPage(); exit(); } else $errmsg = 'You must select a form to be shown'; } else $errmsg = 'You must select a form to be shown'; DisplayPage(); exit(); } else { // // first time for this form // DisplayPage(); exit(); } //**************** //**************** //**************** function DisplayPage() { // define vars that have been created outside of functions global $errmsg; // output the header portion of the html page echo " <!DOCTYPE html> <html> <head> <title>Choose a Form</title> <!-- ****** CSS GOES HERE ******* --> <!-- ****** CSS GOES HERE ******* --> <!-- ****** CSS GOES HERE ******* --> <style type='text/css'> #err_box { position:relative; float:left; margin:1% 3%; width:40%; border:2px solid black; padding:10px; color:red; } #form_box { position:relative; float:left; clear:left; margin:1% 3%; width:40%; border:2px solid blue; padding:10px; } </style> </head> <body> "; // check if there is an error message if ($errmsg) echo " <div id='err_box'> $errmsg </div> "; // // now begin the meat of the page // echo " <div id='form_box'> <form method='POST' action=''> Choose which form you want to see by clicking on a button. <br><br> <label>Show Form #1 <input type='radio' name='form_sel' value='1'> </label> <label>Show Form #2 <input type='radio' name='form_sel' value='2'> </label> <br><br> <center> <input type='submit' name='btn' value='Show Form'> <input type='submit' name='btn' value='Return to Caller'> </center> </form> </div> "; // // now output the tail end of the html page // echo " </body> </html> "; return; // return to the mainline portion of this script } You can take this entire block of code and save it as a .php file and upload it to your server and call it to see how it works. Call it a xmas present. Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 27, 2021 Author Share Posted December 27, 2021 JG, I am not trying to be difficult but, with much advice from you, I have solved my initial problem with ONE issue remaining. One of my forms needed to have the .php extension rather than .html since I was using a php specific function on that page. As a result of changing to .php I have lost the styling on a button I had on that page using a css style sheet. So, now the question becomes, How do I style a button on a .php page? I have been searching all day for an answer to this but none seem to work. What must be changed in order to use the same style sheet on the .php form? Here is what is in my css file: .buttonred { border: 1px solid red; background: #ff8040; border-radius: 30px; font-size:90% ; padding-left: 10px; padding-right: 10px; -webkit-border-radius: 30px; -khtml-border-radius: 30px; -border-radius: 0px; } Thanks again for any advice on this one. David Abineri Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 27, 2021 Share Posted December 27, 2021 11 hours ago, DavidAbineri said: One of my forms needed to have the .php extension I hope you meant to say "scripts" and not "forms". Show us where you assign this class in this script. Perhaps the entire <head> and <form> portions of the file even. And as a habit I do think that pretty much all code you write will go into a filename ending in php. Only if it has no php at all would you use a JS or HTML extension. Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted December 27, 2021 Solution Share Posted December 27, 2021 Follow-up If the class you posted is EXACTLY the same as what you are executing I think you either botched the html that references it or you messed up the css portion of your script. I pasted it into my script (that I gave you) and the submit tags show up nicely formatted. Quote Link to comment Share on other sites More sharing options...
DavidAbineri Posted December 28, 2021 Author Share Posted December 28, 2021 JG, All is working now as it should thanks to your patience with me and all the hints and code you have so willingly supplied. This has been a wonderful lesson in php coding for me thanks to you. Have a great 2022 David Abineri Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 28, 2021 Share Posted December 28, 2021 HTH.. Stay in touch! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.