Solved

Get query and move to next page.

Posted on 2003-11-18
10
289 Views
Last Modified: 2013-12-12
I think this is simple but I'm going out of my mind trying to remember or figure this out.  I've got a form on one page with 3 text boxes.  They're called custnumtb, ordernumtb and sctb.  When the user presses the submit button I want it to check to make sure those boxes have been filled out and then use that information to query the database and return the values on a seperate page.  If the query returns no records I want them to go to another page.  Here is the code I have so far.


<html>

<head>
<title></title>
</head>

<p align="center"><b><i><font size="6">CSP</font></i></b><font size="6">
SUBSCRIBER PAGE</font></p>
<p align="left">&nbsp;</p>
<p align="center"><a href="/new.html">Click here if you are a new subscriber.</a></p>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="75%" id="AutoNumber1">
    <tr>
      <td width="100%">If you would like to renew your existing subscription
      please enter in your customer number and 2 digit state code from your mailing
      label below and click renew.</td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  </center>
</div>

<?PHP

if ($Renew) {

      if (!$ORDNUMTB || !$CUSTNUMTB || !$SCTB){
      
      $error="Sorry!  Your have not filled out the required fields!";
      
      }else{
      
      $db=mysql_connect("localhost", "web", "***");
      
      mysql_select_db("pmgdata_rencspsub", $db);
      
      $sql="SELECT * FROM pmgdata_rencspsub WHERE ORDNUM=$ORDNUMTB AND CUSTNUM=$CUSTNUMTB AND STATE=$SCTB";
      
      $result=mysql_query($sql);
      
HERE IS WHERE I GOT STUCK

<form name="renewsub" action="<?PHP echo $PHP_SELF ?>" form method="POST">
<p align="center">
  CUSTOMER NUMBER<input type="text" input name="ORDNUMTB" size="10">-<input type="text" input name="CUSTNUMTB" size="10">
  STATE CODE <input type="text" input name="SCTB" size="2"></p>
  <p align="center"><input type="submit" id="Renew" value="Renew" name="RENEWBTN">&nbsp;
  <input type="reset" id="Clear" value="Clear" name="CLEARBTN"></p>
</form>
<p>&nbsp;</p>

</body>

</html>
0
Comment
Question by:tnkrtrn
10 Comments
 
LVL 1

Assisted Solution

by:cifroes
cifroes earned 100 total points
ID: 9775267
I guess your code is missing some lines of code, maybe you didn't pasted everything, this way is a bit harder to solve your problem but:

Please don't use ORDNUM=$ORDNUMTB  instead you should use $_POST['ORDNUMTB'] to access $ORDNUMTB, before the $sql you could do $ORDNUMTB = $_POST['ORDNUMTB']

In your query use WHERE ORDNUM='$ORDNUMTB'  , notice the ' ' , not very big issue but sometimes it gives me problems.

But i don't understand
if ($Renew) {

What is $Renew? are you expecting it to be something related to <input type="submit" id="Renew" value="Renew" name="RENEWBTN"> ? because it's not...

If that is the problem just replace:
<form name="renewsub" action="<?PHP echo $PHP_SELF ?>" form method="POST">
to
<form name="renewsub" action="?action=renew" form method="POST">

then replace
if ($Renew) {
to
if ($_GET['action'] == "renew")

and i guess you're ok :)
0
 
LVL 5

Accepted Solution

by:
Karitz earned 250 total points
ID: 9776571
Replace this section of code

if ($Renew) {

     if (!$ORDNUMTB || !$CUSTNUMTB || !$SCTB){
     
     $error="Sorry!  Your have not filled out the required fields!";
     
     }else{
     
     $db=mysql_connect("localhost", "web", "***");
     
     mysql_select_db("pmgdata_rencspsub", $db);
     
     $sql="SELECT * FROM pmgdata_rencspsub WHERE ORDNUM=$ORDNUMTB AND CUSTNUM=$CUSTNUMTB AND STATE=$SCTB";
     
     $result=mysql_query($sql);


with


if ($_POST['Renew'])
{
$ORDNUMTB=$_POST['ORDNUMTB'];
$CUSTNUMTB=$_POST['CUSTNUMTB'];
$SCTB=$_POST['SCTB'];

     if (!$ORDNUMTB || !$CUSTNUMTB || !$SCTB){
     
     $error="Sorry!  Your have not filled out the required fields!";
     
     }else{
     
     $db=mysql_connect("localhost", "web", "***");
     
     mysql_select_db("pmgdata_rencspsub", $db);
     
     $sql="SELECT * FROM pmgdata_rencspsub WHERE ORDNUM=$ORDNUMTB AND CUSTNUM=$CUSTNUMTB AND STATE=$SCTB";
     
     $result=mysql_query($sql) or die(mysql_error().$sql);

//Please post the errors of any for further assistance
0
 

Author Comment

by:tnkrtrn
ID: 9776606
I apologize for not being clear.  After I got home and took a look at my post I couldn't believe how unclear I was being.  I also noticed a ton of mistakes I had made.  Anyway I fixed some of my mistakes (at least the ones I knew I had screwed up on) and will post the whole thing below.  Let me take a second and explain what I'm trying to accomplish.  There are 3 pages.  The index page, a renew page and a new page.  On the index page you can click the link that takes you to the new page.  No problem there.  Also on the index page are 3 text boxes.  CUSTNUMTB, ORDNUMTB and SCTB.  I want people to put their information into those boxes and click the renew button.  Then the renew page should appear with serveral text boxes prepopulated (FNAME, LNAME, STREET1, ect...).  Then on the renew page there is a submit button that they click (after verifing their address) to submit the data to a new table.  I don't have a problem submitting the data on the renew and new page to a table.  The trouble I am having is getting the data on the index page (CUSTNUMTB, ORDNUMTB and SCTB) and using that information to query a seperate table and using the returned data to prepopulate a form on the renew page.  I'm getting even more frustrated so I'm upping the points to 350.

<html>

<head>
<title>CSP SUBSCRIBER PAGE</title>
</head>

<body bgcolor="#E5E5E5">

<p><img border="0" src="csplogo.gif" width="169" height="43"></p>
<p align="center"><b><i><font size="6">CSP</font></i></b><font size="6">
SUBSCRIBER PAGE</font></p>
<p align="left">&nbsp;</p>
<p align="center"><a href="/csp/new.php">Click here if you are a new subscriber.</a></p>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="75%" id="AutoNumber1">
    <tr>
      <td width="100%">If you would like to renew your existing subscription
      please enter in your customer number and 2 digit state code from your mailing
      label below and click renew.</td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  </center>
</div>

<?PHP

if ($RENEWBTN) {

      if (!$ORDNUMTB || !$CUSTNUMTB || !$SCTB) {
      
      $error="Sorry!  Your have not filled out the required fields!";
      
      }else{
      
      $db=mysql_connect("localhost", "pmgdata_web", "********");
      
      mysql_select_db("pmgdata_csp", $db);
      
      $sql = "SELECT * FROM rencspsub WHERE 'CUSTNUM' = $CUSTNUMTB AND 'ORDNUM' = $ORDNUMTB AND 'STATE' = $SCTB";
      
      $result = mysql_query($sql);
      
      $myrow = mysql_fetch_array($result);
      
      }
      
      }
      
      if (!$RENEWBTN || $error) { ?>
      
      <p align="center"><font color="#FF0000">
      
      <?PHP
      
      echo $error;
            
      ?>
      
      </font>
      
<form name="renewsub" action="renew.php" form method="POST">
<p align="center">
  CUSTOMER NUMBER<input type="text" value="<?PHP echo $CUSTNUMTB; ?>" name="CUSTNUMTB" size="10">-<input type="text" value="<?PHP echo $ORDNUMTB; ?>" name="ORDNUMTB" size="10">
  STATE CODE <input type="text" value="<?PHP echo $SCTB; ?>" name="SCTB" size="2"></p>
  <p align="center"><input type="submit" value="Renew" name="RENEWBTN">&nbsp;
  <input type="reset" id="Clear" value="Clear" name="CLEARBTN"></p>
</form>
<?PHP

}

?>
<p>&nbsp;</p>

</body>

</html>




cifroes I read your response but unfortunetly I'm not familier with the $_POST command at all so I'm not sure what that means.  Also I screwed up and put in $Renew when it should have been $RENEWBTN.
0
 
LVL 5

Expert Comment

by:Karitz
ID: 9776649
For more information on $_POST and other server variables, consult the php manual on the areas of

Variables from outside PHP

and

Dealing with Forms

That should give you an introductory tutorial on how to pass variables from one page to another
0
 
LVL 1

Expert Comment

by:Muuss
ID: 9777503
This would work if u have set register_globals to ON in your php.ini
If its set to OFF, use $HTTP_POST_VARS["varname"] instead of $varname

example : $HTTP_POST_VARS["CUSTNUMTB"] instead of $CUSTNUMTB

I hope this will solve your problem.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:tnkrtrn
ID: 9781685
Thanks for the info on the $_POST.  I read a small tutorial and it makes total sense.  In fact I've changed my page around so that there is no connection to the database on this page.  I'll simply use the $_POST on the next page to connect to the database.  Below is the code I have now.  I've got one problem on this page and I'm not sure how to do something on the next page.  On this page I even if there isn't anything in $ORDNUMTB or  $CUSTNUMTB or $SCTB it still keeps going to the next page (test.php in this case) rather than displaying the same page with the error message.  My other question has to do with the next page.  I plan on using the variables in $_POST to query the database.  If no records are found I want it to go to a different page.  Thanks for all the help guys.

<html>

<head>
<title>CSP SUBSCRIBER PAGE</title>
</head>

<body bgcolor="#E5E5E5">

<p><img border="0" src="csplogo.gif" width="169" height="43"></p>
<p align="center"><b><i><font size="6">CSP</font></i></b><font size="6">
SUBSCRIBER PAGE</font></p>
<p align="left">&nbsp;</p>
<p align="center"><a href="/csp/new.php">Click here if you are a new subscriber.</a></p>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="75%" id="AutoNumber1">
    <tr>
      <td width="100%">If you would like to renew your existing subscription
      please enter in your customer number and 2 digit state code from your mailing
      label below and click renew.</td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  </center>
</div>

<?PHP

if ($RENEWBTN) {

      if (!$ORDNUMTB || !$CUSTNUMTB || !$SCTB) {
      
      $error="Sorry!  Your have not filled out the required fields!";
      
      }
      
      }
      
      if (!$RENEWBTN || $error) { ?>
      
      <p align="center"><font color="#FF0000">
      
      <?PHP
      
      echo $error;
            
      ?>
      
      </font>
      
<form name="renewsub" action="test.php" form method="POST">
<p align="center">
  CUSTOMER NUMBER<input type="text" value="<?PHP echo $CUSTNUMTB; ?>" name="CUSTNUMTB" size="10">-<input type="text" value="<?PHP echo $ORDNUMTB; ?>" name="ORDNUMTB" size="10">
  STATE CODE <input type="text" value="<?PHP echo $SCTB; ?>" name="SCTB" size="2"></p>
  <p align="center"><input type="submit" value="Renew" name="RENEWBTN">&nbsp;
  <input type="reset" id="Clear" value="Clear" name="CLEARBTN"></p>
</form>
<?PHP

}

?>
<p>&nbsp;</p>

</body><p></p>

</html>
0
 

Author Comment

by:tnkrtrn
ID: 9785037
I've found a different solution for my first problem.  I've decided to use java.  Works like a charm.  However I'm still not sure what to do on my next page.  

$sql="SELECT * FROM pmgdata_rencspsub WHERE ORDNUM=$ORDNUMTB AND CUSTNUM=$CUSTNUMTB AND STATE=$SCTB";
   
     $result=mysql_query($sql) or die(mysql_error().$sql);

If this sql query returns no records how do i push the user to a new web page.

If anyones interested in how I solved the first part of my problem I'd be happy to post my code here.
0
 
LVL 5

Expert Comment

by:Karitz
ID: 9785115
lemmi see the code

for the other part you can use the header() command

$sql="SELECT * FROM pmgdata_rencspsub WHERE ORDNUM=$ORDNUMTB AND CUSTNUM=$CUSTNUMTB AND STATE=$SCTB";  
$result=mysql_query($sql) or die(mysql_error().$sql);

if (mysql_num_rows($results)<1)
{
  header('location:UrlToNewWebPage');
}
0
 

Author Comment

by:tnkrtrn
ID: 9789734
Here is the code I ended up using.  I'll try your suggestion for the other part.  This might not be the best place to ask this and if it can't be answered I understand.  When I run this page if you don't fill out the required fields a message box pops up and says "The following fields are required: nameoffield".  When you click ok it reloads the page.  Unfortunetly if you fill out 2 out of three fields it deletes the information you put in the two fields and the user has to fill them out again.  Is there anyway around that.

I noticed that if I plug in the action="test.php" right into the form proporties and click OK when the error message pops up it trys to go to test.php.  So thats why I have the line renewsub.action = "test.php" in the script.

<html>
<head>
<title>CSP SUBSCRIBER PAGE</title>
<SCRIPT LANGUAGE="JavaScript">
function verify() {
var error = "The following fields are required: ";
if (document.renewsub.CUSTNUMTB.value=="" || document.renewsub.ORDNUMTB.value=="") {
error = error + "Customer Number";
}
if (document.renewsub.SCTB.value=="") {
error = error + " - State Code";
}
if (error == "You are required to complete the following fields: ") {
document.renewsub.submit();
renewsub.action = "test.php"
}
else {
alert(error);
return false;
   }
}
</script>
</head>
<body bgcolor="#E5E5E5">
<p><img border="0" src="csplogo.gif" width="169" height="43"></p>
<p align="center"><b><i><font size="6">CSP</font></i></b><font size="6">
SUBSCRIBER PAGE</font></p>
<p align="left">&nbsp;</p>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="75%" id="AutoNumber1">
    <tr>
      <td width="100%">If you would like to renew your existing subscription
      please enter in your customer number and 2 digit state code from your mailing
      label below and click renew.</td>           
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  </center>
</div>
<form name="renewsub" form method="POST">
<p align="center">
  CUSTOMER NUMBER<input type="text" name="CUSTNUMTB" size="10">-<input type="text" name="ORDNUMTB" size="10">
  STATE CODE <input type="text" name="SCTB" size="2"></p>
  <p align="center"><input type="submit" value="Renew" name="RENEWBTN"onclick="verify();">&nbsp;
  <input type="reset" id="Clear" value="Clear" name="CLEARBTN"></p>
</form>
<p>&nbsp;</p>
<p align="center"><a href="new.php">Click here if you are a new subscriber.</a></p>
</body><p></p>
</html>
0
 

Author Comment

by:tnkrtrn
ID: 9811877
Well I'm estatic.  All my questions have been answered.  Most of the response here let me to more questions but thats a good thing.  I was able to research specific points and come away with a greater understanding of php.  Anyway I'm going to split the points here.  100 for cifroes for suggesting the $_POST and 250 for Karitz for telling me what subject to search for when researching $_POST and for the addtional php and javascript help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now