Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2011, 06:07 AM   PM User | #1
jeggerd
New to the CF scene

 
Join Date: Nov 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jeggerd is an unknown quantity at this point
PHP printing queries results for submits not asked

Hi, so i have an html page that has multiple forms in it. Some of those forms can take an empty string to run a query in the php if no text is entered into it.
These forms work fine, my only issue is that if I am clicking on a form elsewhere, say $query4, then the results page will print that, as well as $query, $query6, and $query7 since they can take empty strings.

How can i fix my code so the Php will only print the query for the specific submit button that is clicked? I have heard about if(isset($_POST['submit'])) but i'm not sure where in my code that would go because when I tried it, it makes my queries return empty.

Thanks a lot!

here is my php:
PHP Code:
 
$_POST['submit_query'] == "submit_query";
$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : NULL;
$lastName= mysql_real_escape_string($lastName);

$query = "SELECT c.*, s.speaker_year FROM Contact c, Speakers s WHERE s.Contact_con_id = c.con_id
AND con_lname =  ";
$query .= "'" . $lastName . "' ORDER BY con_lname;";
if ($lastName == ""){
    $query = "SELECT c.*, s.speaker_year FROM Contact c, Speakers s WHERE s.Contact_con_id = c.con_id ";
}

?>


<?php

$state 
$_POST['state'];
$state mysql_real_escape_string($state);

$query1 "Select proposal_id, proposal_status, proposal_title, proposal_description, con_fname, con_affiliation, con_city, role
from Contact c, Proposal p, Presents px
WHERE px.Speakers_Contact_con_id = c.con_id
AND px.Proposal_proposal_id = p.proposal_id
AND c.con_state = "
;
$query1 .= "'" $state "' ORDER BY p.proposal_id;";
?>

<?php

$review 
$_POST['review'];
$review mysql_real_escape_string($review);

$query2 "Select con_fname, con_lname con_id, proposal_id, proposal_title
from Contact c JOIN Individual_Review ir JOIN Proposal p
WHERE ir.Reviewer_Contact_con_id = c.con_id
AND ir.Proposal_proposal_id = p.proposal_id
AND con_fname = "
;
$query2 .= "'" $review "' ORDER BY c.con_fname;";
?>

<?php

$mail 
$_POST['mail'];
$mail mysql_real_escape_string($mail);

$query3 "Select c.con_fname, c.con_lname, c.con_street_addy, c.con_city, c.con_state, c.con_zip,
con_affiliation, p.proposal_title, p.proposal_status
from Contact c, Proposal p, Presents px
WHERE px.Speakers_Contact_con_id = c.con_id
AND px.Proposal_proposal_id = p.proposal_id
and px.role = "
;
$query3 .="'" $mail "' ORDER BY c.con_fname;";
?>

<?php

$rtype 
$_POST['rtype'];
$rtype mysql_real_escape_string($rtype);

$query4 "Select r.reviewer_type, c.*
from Contact c, Reviewer r
WHERE r.Contact_con_id = c.con_id
AND reviewer_type = "
;
$query4 .= "'" $rtype "' ORDER BY r.reviewer_type;";
?>

<?php

$average 
$_POST['average'];
$average mysql_real_escape_string($average);

$query5 "SELECT c.con_fname, r.Reviewer_Contact_con_id, question_id, AVG( DISTINCT question_score)
FROM Contact c, Individual_Review r
WHERE r.Reviewer_Contact_con_id = c.con_id
AND con_fname = "
;
$query5 .= "'" $average "' GROUP BY r.Proposal_proposal_id;";
?>

<?php

$rgroups 
= isset($_POST['rgroups']) ? $_POST['rgroups'] : "";
$rgroups mysql_real_escape_string($rgroups);


$query6 "SELECT r.rev_groups_id, c.con_fname, c.con_lname, c.con_phone, r.rev_groups_pass, count(p.proposal_id)
FROM Review_Groups r JOIN Proposal p on r.rev_groups_id = p.Review_Groups_rev_groups_id
JOIN Presents px on px.Proposal_proposal_id = p.proposal_id
JOIN Contact c on px.Speakers_Contact_con_id = c.con_id
JOIN Reviewer rw on rw.Review_Groups_rev_groups_id = r.rev_groups_id
WHERE rw.reviewer_type = 'Local'"
;

if (
$rgroups == "") {
    
$query6 .= " Group BY r.rev_groups_id;";        
}
Else {
    
$query6 .="AND r.rev_groups_id = '" $rgroups "'";
}
?>

<?php


$check 
$_POST['check'];
$check mysql_real_escape_string($check);

$query7 "Select c.con_fname, c.con_lname, s.Contact_con_id,
IF(s.Contact_con_id IS NULL, 'NO', 'YES')
From Contact c Left Join (Select Contact_con_id FROM Speakers
WHERE speaker_year = '"
$check "') As s
ON c.con_id = s.Contact_con_id"
;
$query7 .= " ORDER BY c.con_fname;";
?>


<p>
The query:
<p>
<?php
print $query;
?>
</br></br>
<?php
print $query1;
?>
</br></br>
<?php
print $query2;
?>
</br></br>
<?php
print $query3;
?>
</br></br>
<?php
print $query4;
?>
</br></br>
<?php
print $query5;
?>
</br></br>
<?php
print $query6;
?>
</br></br>
<?php
print $query7;
?>
<hr>


<p>
Result of query:
<p>

<?php
$result 
mysql_query($query)or die(mysql_error());
$result1 mysql_query($query1)or die(mysql_error());
$result2 mysql_query($query2)or die(mysql_error());
$result3 mysql_query($query3)or die(mysql_error());
$result4 mysql_query($query4)or die(mysql_error());
$result5 mysql_query($query5)or die(mysql_error());
$result6 mysql_query($query6)or die(mysql_error());
$result7 mysql_query($query7)or die(mysql_error());



print 
"<pre>";

while(
$row mysql_fetch_array($result))
  {
  
    print 
"\n";
    print
"{$row[0]} {$row[1]} {$row[2]}, {$row[3]}, {$row[4]}, {$row[5]}, {$row[6]}
    {$row[7]} {$row[8]} {$row[9]} {$row[10]} {$row[11]}"
;
  }

while(
$row1 mysql_fetch_array($result1))
  {
    print 
"\n";
    print 
"{$row1[0]} {$row1[1]} {$row1[2]}, {$row1[3]}, {$row1[4]}, {$row1[5]}, {$row1[6]}
    {$row1[7]}"
;
  }
while(
$row2 mysql_fetch_array($result2))
  {
    print 
"\n";
    print 
"{$row2[0]} {$row2[1]} {$row2[2]}, {$row2[3]}";
  }
while(
$row3 mysql_fetch_array($result3))
  {
    print 
"\n";
    print 
"{$row3[0]} {$row3[1]} {$row3[2]} {$row3[3]}, {$row3[4]}, {$row3[5]}, {$row3[6]}
    {$row3[7]} {$row3[8]}"
;
  }
while(
$row4 mysql_fetch_array($result4))
  {
    print 
"\n";
    print 
"{$row4[0]} {$row4[1]} {$row4[2]} {$row4[3]}, {$row4[4]}, {$row4[5]}, {$row4[6]}
    {$row4[7]} {$row4[8]} {$row4[9]} {$row4[10]} {$row4[11]} {$row4[12]}"
;
  }
while(
$row5 mysql_fetch_array($result5))
  {
    print 
"\n";
    print 
"{$row5[0]} {$row5[1]} {$row5[2]}, {$row5[3]}";
  }
while(
$row6 mysql_fetch_array($result6))
  {
    print 
"\n";
    print 
"{$row6[0]} {$row6[1]} {$row6[2]}, {$row6[3]}, {$row6[4]}, {$row6[5]}";
  }
  
while(
$row7 mysql_fetch_array($result7))
  {
    print 
"\n";
    print 
"{$row7[0]} {$row7[1]} {$row7[2]} {$row7[3]}";
  }
print 
"</pre>"

Last edited by jeggerd; 11-28-2011 at 06:19 AM.. Reason: adding info
jeggerd is offline   Reply With Quote
Old 11-28-2011, 07:46 AM   PM User | #2
vivekgg7
New to the CF scene

 
Join Date: Nov 2011
Location: pune, india
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
vivekgg7 is an unknown quantity at this point
try with html iframe
vivekgg7 is offline   Reply With Quote
Old 11-28-2011, 08:07 AM   PM User | #3
jeggerd
New to the CF scene

 
Join Date: Nov 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jeggerd is an unknown quantity at this point
how would iframe help me?
jeggerd is offline   Reply With Quote
Old 11-28-2011, 10:34 AM   PM User | #4
vivekgg7
New to the CF scene

 
Join Date: Nov 2011
Location: pune, india
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
vivekgg7 is an unknown quantity at this point
Smile

Quote:
Originally Posted by vivekgg7 View Post
try with html iframe
set each html page in each i frame
try like this,

Creating a multi-page order form using PHP

in PHP Form

Forms are part of virtually any web application today. They are the main method to receive input from people using the application. They range in size from one-field opt-in forms where you only enter your email address, to very long forms with tens or even hundreds of fields.

To make long forms user-friendlier, it is a good idea to span the form on multiple pages. This can make it easier to follow for the user, and we can also split the data in separate sections, based on the scope (for example separate personal customer information from payment data in a shopping cart checkout form).

One of the challenges that arise from splitting the form over multiple pages is passing the data from one page to another, as at the final point of the form, we have all the needed data ready for processing. We are going to look at two methods to do this: session variables and hidden input fields.
What are sessions anyway?

A HTML session is a collection of variables that keeps its state as the user navigates the pages of a certain site. It will only be available to that domain that created it, and will be deleted soon after the user left the site or closed his browser.

So, the session has a semi-permanent nature, and it can be used to pass variables along different pages on which the visitor lands during a visit to the site.
Multi-page form using sessions

In our example, we are going to create a three pages form, resembling a membership signup and payment form. The first page will ask for customer's name and address, on the second page there is the choice for membership type, and on the third and final page, payment data must be entered. Final step is saving the data in MySQL.

The first file we are going to create (step 1 of the form) will contain just a simple form with two fields.<form method="post" action="form2.php">
<input type="text" name="name">
<input type="text" name="email_address">
<input type="submit" value="Go To Step 2">
</form>


Ok, so nothing more than 2 input fields and a submit button to take us to step 2. In the following page, apart from the HTML form to gather membership data, we are going to need code to store the submitted data from step 1 in the session.
PHP Code:
<?php
//let's start the session
session_start();
//now, let's register our session variables
session_register('name');
session_register('email_address');
//finally, let's store our posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['email_address'] = $_POST['email_address'];
?>
<form method="post" action="form3.php">
<input type="radio" name="membership_type" value="Free">
<input type="radio" name="membership_type" value="Normal">
<input type="radio" name="membership_type" value="Deluxe">
<input type="checkbox" name="terms_and_conditions">
<input type="submit" value="Go To Step 3">
</form>


On to step 3, we must again create new session variables, assign values received by post, and create the final part of the form.<?php
//let's start the session
session_start();
//now, let's register our session variables
session_register('membership_type');
session_register('terms_and_conditions');
//finally, let's store our posted values in the session variables
$_SESSION['membership_type'] = $_POST['membership_type'];
$_SESSION['terms_and_conditions'] = $_POST['terms_and_conditions'];
?>
<form method="post" action="form_process.php">
<input type="text" name="name_on_card">
<input type="text" name="credit_card_number">
<input type="text" name="credit_card_expiration_date">
<input type="submit" value="Finish">
</form>


Now that we created step 3 of the form, what's left is the final processing script that inserts the data in the MySQL database.<?php
//let's start our session, so we have access to stored data
    
session_start();
//let's create the query
$insert_query 'insert into subscriptions (
                name,
                email_address,
                membership_type
                terms_and_conditions,
                name_on_card,
                credit_card_number,
                credit_card_expiration_date
                        ) values (
                " . $_SESSION['
name'] . ",
                " . $_SESSION['
email_address'] . ",
                " . $_SESSION['
membership_type'] . ",
                " . $_SESSION['
terms_and_conditions'] . ",
                " . $_POST['
name_on_card'] . ",
                " . $_POST['
credit_card_number'] . ",
                " . $_POST['
credit_card_expiration_date'] . "
                        );
//let'
s run the query
mysql_query
($insert_query);
?>

Last edited by Inigoesdr; 11-28-2011 at 07:39 PM..
vivekgg7 is offline   Reply With Quote
Reply

Bookmarks

Tags
html, php, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:36 PM.


Advertisement
Log in to turn off these ads.