two forms conflict with each other - php

I have placed two forms on one page. Bothe forms work fine separately but when they are placed on one page at the same time they conflict with each other. Here are both forms:
Contact Form:
<form name="contactform" id="contactform" method="post" action="#targetAnchorPage2">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
<td colspan="2" class="error">
<?php
require_once("contact_send.php");
?>
</td>
</tr>
<?php
}
?>
<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>
<tr><td colspan="2"><label id="tworows" for="message" class="<?=$message_class?>">your message:</label></td></tr><tr><td colspan="2"><textarea name="message" cols="22" rows="6" value="<?=$message_class?>"></textarea>
</td></tr>
<tr>
<td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">
</td>
</tr>
</table>
</form>
Subscribe Form:
<form name="subscribeform" id="subscribeform" method="post" action="#targetAnchorPage3">
<table>
<?php
if (isset($_POST["name"])){
?>
<tr>
<td colspan="2" class="error">
<?php
require_once("subscribe_send.php");
?>
</td>
</tr>
<?php
}
?>
<tr><td><label for="name" class="<?=$name_class?>">name:</label></td><td><input type="text" name="name" maxlength="50" value="<?=$name?>"></td></tr>
<tr><td><label for="email" class="<?=$emailaddress_class?>">email:</label></td><td><input type="text" name="email" maxlength="80" value="<?=$emailaddress?>"></td></tr>
<tr>
<td colspan="2" style="text-align:center"><br /><input class="button" type="submit" value="">
</td>
</tr>
</table>
</form>
How can this be solved? Is it caused by the "required_once" command?

I am guessing that since you are showing the required files based on the same criteria isset($_POST['name']) and since both forms have the name field you end up showing the code in both requires regardless of which form is submitted. You should simply change the form field names on on of the forms such that they are different.

Both forms have the same action attribute, they both point back to the same page (note that the hash is not sent to the server). As they both have a field called name and you are checking for that, both actions get executed regardless of which form was sent in.
You can do either:
use different scripts / form processors (don't post back to the same page)
use a different check for each form, for example by adding a hidden input that will allow you to distinguish between the forms.

Add
formaction="Your_URL"
Attribut in Button

Related

Retrieve multiple row value with dynamic id of html table using php

I have a html table with multiple rows with dynamic Id and name assigned. I want to retrieve the rows values in php. When I am doing the same, it is showing: undefined index error. The code is as below, pls help where I am wrong
<?php
if(isset($_POST["hdnsubmitted"])
{
for ($i=0;$i<6;$i++)
{
$curr_status=$_POST['curr_status'.$i]; echo $curr_status;
}
$Status= "Status Updated";
}
?>
<form method="post" action="targetpage">
<table class="freeze-table">
<thead>
<tr >
<th class="col-id-no fixed-header" >CURRENT STATUS</th>
<th class="col-id-no fixed-header" >PENDING INPUTS</th>
</tr>
</thead>
<?php
$query="SELECT * FROM `status` WHERE 1";
$sql=mysqli_query($conn,$query);
$i=0;
while ($row=mysqli_fetch_array($sql)){
echo "<tr>
<td name='Curr_status'".$i."><div contenteditable> ".$row['CURRENT_STATUS']." </td>
<td name='Pending_Inputs'".$i."><div contenteditable> ".$row['PENDING_INPUTS']." </td>";
$i=$i+1;
}
?>
</tr>
</table>
<br>
<button type="submit" name="btn_Update" class="btn_Update">Update</button>
<p style="color:black;text-align: center"> <?php echo $Status; ?></p>
</form>
You don't have any form. You have a table and table contents do not sent to php.
You must use a form with fields. Each field must have a name.
In below simplest example: method is post so your form content will be in $_POST. In targetpage.php page, try var_dump($_POST) to see the result after you submit the form.
You'll see $_POST is an array with these keys: email, name, submit.
form.php // php page that has the form
<form action="targetpage.php" method="post">
<div class="form-example">
<label for="name">Enter your name: </label>
<input type="text" name="name" id="name" required>
</div>
<div class="form-example">
<label for="email">Enter your email: </label>
<input type="email" name="email" id="email" required>
</div>
<div class="form-example">
<input type="submit" value="Subscribe!">
</div>
</form>

<input> element does not post data?

I have a form page and when I submit this form all inputs are posting successfully except this one:
<input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()"
<?php if($this->data['kullanici_id']){echo 'readonly';} ?>
value="<?php echo $this->data['kullanici_id']?>">
But why?
-This is my .phtml file:
<html>
<head>
</head>
<body>
<form enctype="multipart/form-data" action="/admin/kaydet" method="post" onSubmit="javascript: beforeSubmit();">
<?php if(strlen($this->data['id'])):?>
<input type="hidden" name="id" value="<?php echo $this->data['id']?>">
<?php endif;?>
<font color="green"><h3>DÜZENLE</h3></font>
<img src="/foto/<?php echo $this->data['fotograf']?>" height="110" width="110" align="left" />
<table class="table">
<tr>
<td>T.C. Kimlik No.:</td>
<td><input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()" <?php if($this->data['kullanici_id']){echo 'readonly';} ?> value="<?php echo $this->data['kullanici_id']?>"></td>
</tr>
<?php if(!strlen($this->data['id'])):?>
<tr>
<td>Parola:</td>
<td><input id="password2" class="form-control" type="password" name="parola" value="<?php echo $this->data['parola']?>"></td>
</tr>
<tr>
<td>Parola Tekrar:</td>
<td><input onchange="passwordCheck(this.value)" class="form-control" type="password" name="parola" value="<?php echo $this->data['parola']?>"></td>
</tr>
<?php endif; ?>
</table>
<td><button type="submit" class="btn btn-success btn-sm glyphicon glyphicon-floppy-disk">KAYDET</button> </td>
</form>
</body>
</html>
If I have an id; page looks like an edit member page, if I haven't; page will add a new member. In id="TC" input, if I edit a member, this input shouldn't change, so I add a readonly to solve this. But when I submit, input does not post.
Sorry about my bad English :D
Reason your field is not being submitted it is because its set to readonly.
Reason for this is 'If user cannot change the field there is no point of submitting it as value will always remain the same'.
One way to mitigate this behavior is to add hidden field with same name
<input type="hidden" name="kullanici_id" value="<?php echo $this->data['kullanici_id']?>"> />
<input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()" <?php if($this->data['kullanici_id']){echo 'readonly';} ?> value="<?php echo $this->data['kullanici_id']?>" />

How to submit two php forms to one location

I have two php forms, both for registration means (enter name, username, password etc). My first registration form (reg1.php) contains the basic inputs as I mentioned above and then I have a second registration form (reg2.php) that includes checkboxes where the form would ask the user other questions before finalizing their sign up.
What I am having trouble with is that when I enter data into my inputs in the first registration form, then proceed to the next registration page and fill that out and click Sign Up, my output on my server only obtains the information I inputted in the second registration page, not the first.
How can I get both inputted data from the two registration pages to appear together? I have been told about POSTBACK to help with this but I am not all that familiar with how to use it.
First php page:
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table style="width:300px">
<form name="Rego" method="post" action="rego2.php">
<tr>
<td><label>Name:</label></td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td><label>Username:</label></td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td><label>Password:</label></td>
<td><input type="password" name="password" id="pass" /></td>
</tr>
<tr>
<td><label>Confirm Password:</label></td>
<td><input type="password" name="conpassword" id="conpass"/></td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Next Page">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
Second php page
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
When the first form is submitted either store the data in a $_SESSION variable and retrieve it when you process the second form, or write it to hidden <input> elements on your second form and retrieve it from the $_GET or $_POST array
In 2nd input form you take first inout from value and keep those one as hidden.
and 2nd input form you should
<body>
<?php include("menu.php"); ?>
<div id="main">
<h2>Sign Up</h2>
<table>
<form name="Rego" method="post" action="http://myserver...">
<input type="hidden" name="fname" value="<?php echo $_POST['fname'];?> " />
<input type="hidden" name="username" value="<?php echo $_POST['username'];?> " />
<input type="hidden" name="passowrd" value="<?php echo $_POST['password'];?> " />
<input type="hidden" name="conpassowrd" value="<?php echo $_POST['conpassword'];?> " />
<tr>
<td>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
</td>
</tr>
<tr>
<td><p><input class="button" type="submit" value="Sign Up">
<input class="button" type="reset" value="Reset"></p></td>
</tr>
</form>
</table>
</div>
</body>
I hope it will work for you.
In the second page, you can get the post data from page 1 and set it in a hidden field like this, say the fname:
<input type="hidden" name="fname" id="fname" value="<?php echo $_POST['fname'] ?>">
When you submit from the second page, you can catch this data in the destination page, say
$_POST['fname']

Undefined variable: POST - PHP and MySQL

I've got a form like this:
<form name="htmlform" method="post" action="script/gen.php?postData">
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="customer">Customer:</label>
</td>
<td valign="top">
<input type="text" name="customer" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="nol">Number of licences: </label>
</td>
<td valign="top">
<input type="text" name="nol" maxlength="50" size="30">
</td>
</tr>
<tr>
<td>
<form method="post" id="submit" action="script/gen.php">
<input type="button" onClick="getKey()"; value="Generate key"/>
</td>
</tr>
<div id="innhold">
<h4>Licence Key: </h>
</div>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
The codelines of interest is:
<input type="text" name="nol" maxlength="50" size="30">
and
<input type="text" name="customer" maxlength="50" size="30">
I try to write this information to a database like this:
function postData($key1) {
//just to check if the key is equal to the one thats posted to the user
//echo '<h5>From postData' . $key1 . '</h5>';
/*echo '<script type="text/javascript"> alert("The order has been submitted successfully");
location = "/Webpanel/index.html";
</script>';*/
$customerVar = $POST['customer'];
$nolVar = $POST['nol'];
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("licencedatabase");
$query_add = "INSERT INTO licence (`customer_name`,`licence_count`) VALUES ('$customerVar','$nolVar')";
$query_exec = mysql_query($query_add) or die(mysql_error());
mysql_close();
}
But I keep getting the error:
Undefined variable: POST
How can I accomplish this? Thanks in advance.
Its $_POST not $POST.
$customerVar = $_POST['customer'];
$nolVar = $_POST['nol'];
That's because it's called $_POST.
Try using $_POST instead of $POST
to access the superglobal POST use $_POST not $POST
All PHP superglobals (such as those for GET and POST) are prefixed with an underscore, so: $POST should be $_POST.
Have a look here for more information about the available superglobals in PHP:
http://php.net/manual/en/language.variables.superglobals.php
Try using $_POST instead of $POST
Check following example:
The predefined $_POST variable is used to collect values from a form sent with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
Example:
<form action="submitform.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the user clicks the "Submit" button, the URL will look like this: http://localhost/submitform.php
The "submitform.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
might you will understand clearly.

PHP Validation Message appears on top of my user form

I'm having a problem with one of my PHP form validation scripts.
Basically, I have a form that when a user submits information it does validation checking (via PHP) and outputs the result (Success, Error1, Error2 etc) via an echo statement
However, after a user Submits the form (the action=same page ie. it posts to itself) the validation message appears at the top of the form or next to it. I want the echo message to appear under the form.
This is what is happening now in the page:
Error: Your password is incorrect
Username:
Email:
Password:
Submit Button
This is what I want it to look like:
Username:
Email:
Password:
Submit Button
Error: Your password is incorrect
Does anyone know how to remedy this?
Here is the code:
<form id="username_check" name="username_check" method="post">
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username"
value="<?php echo($_POST['Username']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address"
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</form>
<?php
//If form was submitted
if (array_key_exists('submit',$_POST)){
//Do something
echo "Form validation here.....";
}
?>
This is dependent on the order of execution of the code. If you are using an IDE, set a breakpoint before the validation and step through. Otherwise, I'm pretty sure we'll need to see some of the code to get an idea of why this is happening.
Try:
<form id="username_check" name="username_check" method="post">
<table>
<tr>
<td><b>Username:</b></td>
<td><input name="Username" type="text" class="textfield" id="Username"
value="<?php echo($_POST['Username']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input name="email_address" type="text" class="textfield" id="email_address"
value="<?php echo($_POST['email_address']); ?>"
/>
</td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="PASSWORD" type="password" class="textfield" id="PASSWORD" />
</td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</table>
</form>
<?php
//If form was submitted
if (array_key_exists('submit',$_POST)){
//Do something
echo "Form validation here.....";
}
?>
If you are calling the
echo $error_message;
before showing the form, then the message is displayed before the form.
If you want the error message after the form just move the echo command after the form is printed out.
Sounds like you are echo'ing out your error message, then echo'ing out the form.
Do it the other way around.
Don't echo the error save it in to a variable and print it behind the form print.
That way you can also check every time if an error occur during form validation, simple check if error-variable is emtpy/false

Categories