Jump to content

Deciding which page to load


Go to solution Solved by ginerjm,

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/314348-deciding-which-page-to-load/
Share on other sites

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 by ginerjm

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.

 

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 by ginerjm

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

 

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 by ginerjm

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>
	&nbsp;&nbsp;&nbsp;
	<label>Show Form #2
	<input type='radio' name='form_sel' value='2'>
	</label>
	<br><br>
	<center>
	<input type='submit' name='btn' value='Show Form'>
	&nbsp;&nbsp;&nbsp;
	<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.

 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

 

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...

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

 

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.

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>
		&nbsp;&nbsp;&nbsp;
		<label>Show Form #2
		<input type='radio' name='form_sel' value='2'>
		</label>
		<br><br>
		<center>
		<input type='submit' name='btn' value='Show Form'>
		&nbsp;&nbsp;&nbsp;
		<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.  :)

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

 

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.

  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.