Jump to content

Ned help with PHP forms


rickzwebz

Recommended Posts

On a single php page I have 2 forms. Each form calls to a different mySQL table (ex table1, table2, table3, etc).

When I select 1 form on the page the other form is also called.

Is it possible for just the form chosen to be called and retrieve no information from the other form?

Right now when I click on the submit button all the areas where a form is being used displays results.

I have 1 func.php page with the text as below and call it to any spot I need with an include.

First off is it  possible to use a function, like this, in this manner?

I'm coding in strict so  <form ='name'> is not allowed. In its place I'm using  i d="<?php echo $idLetter ?>"

and then using (on a index.php page)

$idLetter = 'A';  (then using $idLetter = 'B';  , etc)  to differentiate the forms

also using <input type='submit' name='<?php echo $inputName ?>' value='CHECK THIS DATE' />

and then using (on a index.php page)

$inputName = "submit1"; (then using $inputName = "submit2"; , etc)  to differentiate the forms

Those 2 changes for every form do not seem to be enough. It doesn't matter which submit button is clicked on. all forms are called.

 

 

 

 

 

The first part of this text is the func.php and then call it with the second part of this text on any PHP page I wish to use it.

 

 

THIS THE FUNC.PHP

 

 

<form action="<?php echo $PHP_SELF;?>"  method="post" i d="<?php echo $idLetter ?>">

 

<p>Check if this <?php echo $product ?> is available for your event date</p>

 

<p><select name='month'>

<option value=''>Month</option>

<option value='january'>January</option>

......

<option value='november'>November</option>

<option value='december'>December</option>

</select>

 

 

 

<select name='day'>

<option value=''>Day</option>

<option value='1'>1</option>

<option value='2'>2</option>

.....

<option value='30'>30</option>

<option value='31'>31</option>

 

</select></p>

 

<p><input type='submit' name='<?php echo $inputName ?>' value='CHECK THIS DATE' /></p>

 

<?php

// WHEN SUBMIT IS PRESSED

...

 

if (isset($month)&&$month!=""){

 

// GET ALL THE RECORDS FROM THE TABLE ...

 

$result=mysql_query("SELECT * FROM $selectTable WHERE month ='$month' and day ='$day'");

$row=mysql_fetch_assoc($result);

 

 

if ($row['available'] == 'Y') {

echo "<p>This $product is available on: <br /> $month $day</p>

<h2>Book this $product?  

 

<input type='submit' name='submit' value='BOOK NOW' /></h2>";

}else{

echo "<p>Sorry, but this $product is not available on $month $day</p>

<p>Please try another date or choose a different $product.</p>";

}

 

}

 

?>

 

</form>

 

 

 

 

 

 

THIS IS USED ON ANY PHP(XHTML ) PAGE WHERE I WANT TO CALL THE FUNCTION

 

<?php

$idLetter = 'A';

$product = "bouncer";

$inputName = "submit1";

$availabiltyAddress = " selectTable";

include "elements/functions/checkForAvailability.func.php"

?>

Link to comment
Share on other sites

Ok my problem is that the form is in a function and I'm using for every place I need to call it

The function starts with a <form> and ends with a </form>

I am using id (name can't be used for strict) like this : id="<?php echo $idLetter ?>

and then in the php page using : $idLetter = 'A'; (then $idLetter = 'B'; , etc)

 

for submit button

<input type='submit' name='<?php echo $inputName ?>' value='CHECK THIS DATE' /></p>

then on php page

$inputName = 'submit1'; (then $inputName = 'submit2'; , etc

 

Would this work?

Link to comment
Share on other sites

PHP cannot identify forms by their ids. You'll want to give your submit buttons unique names, for example name your submit buttons like form1_submit, form2_submit etc.

 

Now to see which form has been submitted use

 

if(isset($_POST['form1_submit']))
{
    // code for form1 here
}
elseif(isset($_POST['form2_submit']))
{
    // code for form2 here
}

etc

Link to comment
Share on other sites

As has been said, any submit button on a form will always submit all fields btween the same pair of <form> </form> tags as the button.

 

If you want to have a button that submits certain fields only from within a form, then you would have to not use a submit button but a type="button" button and then onclick of that button you would call some javascript to extract the fields you want that button to submit, and send the values to the server via ajax either with GET or POST depending on the volume of data involved.

Link to comment
Share on other sites

Thank-you to all, I appreciate all the replies and have tried them all, but I guess I wasn't to clear on my question. I need the form to open to an area on the same page and not open a new page. As when you click on the submit, just below, will display "yes this is available" or what ever. This  works now, but for every item on the page a result is displayed and not just the item chosen.

Link to comment
Share on other sites

Not sure exactly what you are trying to do but maybe this example will help you. Even though you can fill in both input boxes only the one associated with its own submit button will post data.

<form action="" method=post>
<input name=in1 value=""> <br>
<input type=submit value=form1>
</form>
<form action="" method=post>
<input name=in2 value="">  <br>
<input type=submit value=form2>
</form>
<?php
$in1=$_POST['in1'];
$in2=$_POST['in2'];
echo "1::$in1<br>";
echo "2::$in2";
?>

 

if you want both inputs can have the same name and again only the one associated with its own form will post.

 

HTH

Teamatomic

Link to comment
Share on other sites

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.