Multiple forms on same page - If empty - php

I'm currently in the process of coding a MySQL search page and I'm unable to work out how to make it so that if there's no data put in one form it'll do nothing, and if there's data in the other form it'll search the database for that value.
<?PHP
echo '<h3 class="hp-1">Kick Logs</h3><div class="wrapper">
<div class="logcol-1"><form name="form1" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '">
<input name="name" type="text" id="name" placeholder="Players Name">
</form>';
echo '<form name="form2" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '"><input name="reason" type="text" id="reason" placeholder="Kick Reason"></form>';
$name = mysql_real_escape_string($name);
$reason = mysql_real_escape_string($reason);
$kicklogname = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$name%'") or die(mysql_error());
$kicklogreason = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$reason%'") or die(mysql_error());
if($name == ""){
echo "You must enter a name to search"; }
else {
echo '<table width="700" border="0">
<tr class="listheader">
<td width="100" bgcolor="#afe6ff">Username</td>
<td width="220" bgcolor="#afe6ff">Reason</td>
</tr>';
while($row = mysql_fetch_array($kicklogname))
{
echo '<tr><td bgcolor="#daf4ff" class="contentleft">';
echo $row['user'];
echo '</td><td bgcolor="#eefaff" class="contentright">';
echo $row['reason'];
echo '</td></tr>';
}
echo '</table></div></div>';
}
?>

Update
Sorry, re-read the question. To check multiple forms on the same page you need to add a submit button to each form and give it a name:
<?php
if (!empty($_POST) && isset($_POST['reasonsubmit'])) {
echo 'Submitted reason form';
}
if (!empty($_POST) && isset($_POST['namesubmit'])) {
echo 'Submitted name form';
}
?>
<form action="reason.php" method="post">
<input type="text" name="reason" id="reason" />
<input type="submit" name="reasonsubmit" />
</form>
<form action="name.php" method="post">
<input type="text" name="name" id="name" />
<input type="submit" name="namesubmit" />
</form>
Alternatively, if you didn't want to give the submit button a name you can just add a hidden field to each form and you will get the same behavior.
<form action="reason.php" method="post">
<input type="hidden" name="reasonsubmit" id="reasonsubmit" />
<input type="text" name="reason" id="reason" />
<input type="submit" />
</form>
<form action="name.php" method="post">
<input type="hidden" name="namesubmit" id="namesubmit" />
<input type="text" name="name" id="name" />
<input type="submit" />
</form>

Related

2 different action in one form

I have a form called addcustomer.php . it's contain firsname, lastname, mobile fields.i have also 2 button on this form. onr button for saving data and the others for sending sms to customers.
i want to when i click second button 3 data fields passed to another form called smsinfo.php.
now action for saving data works good but when direct to smsinfo . there is no data on this form .
<form method ="POST" name = "custform" action="">
<div class="cell_2_2"><label class="lblfields">Firstname:</label> <input type="text" name="firstname" autocomplete="off"/></div>
<div class="cell_3_2"><label class="lblfields">Lastname :</label> <input type="text" name="lastname" autocomplete="off"/></div>
<div class="cell_5_2"><label class="lblfields">Mobile :</label> <input type="text" name="mobile" autocomplete="off"/></div>
<input type="submit" name="submit" value="Save"/>
<input type="submit" name="sendsms" value="Sms"/>
if (isset($_POST['submit']))
{
Saving code here
} else if (isset($_POST['sendsms'])) {
header("Location: smsinfo.php");
}
here code of smsinfo.php:
<?php
$Fname = $_REQUEST['firstname'];
$Lname = $_REQUEST['lastname'];
$Mob = $_REQUEST['mobile'];
?>
<html>
<body>
<form action="sendingsms.php" method="POST">
<input style="width: 100px;" name="firstname" type="text" value="<?php echo $firstname; ?>"/>
<input style="width: 100px;" name="lastname" type="text" value="<?php echo $lastname; ?>"/>
<input style="width: 100px;" name="mobile" type="text" value="<?php echo $mobile; ?>"/>
</form>
</body>
</html>
thanks all and sorry for my poor english

How to retrieve value on from in php?

I am trying to retrieve value on php page but it's not retrieving value.
Here is my code
retrieve
<html>
<body>
<?php
include('conn.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
$select_table = "select * from clientreg order by id limit $start,$per_page";
$variable = mysql_query($select_table);
?>
<form name="frmUser" method="post" action="">
<div style="width:100%;">
<table border="0" cellpadding="10" cellspacing="1" width="100%" class="tblListForm">
<tr class="listheader">
<td></td>
<td width="230" >*****</td>
</tr>
<?php
$i=1;
$j=0;
while($row = mysql_fetch_array($variable))
{
if($j%2==0)
$classname="evenRow";
else
$classname="oddRow";?>
<tr class="<?php echo $classname;?>">
<td><input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?>" ></td>
</tr>
<?php
$j++;}
?>
<tr class="listheader">
<td colspan="9"><input type="button" name="update" id="onclick" value="Update" /> <input type="button" name="delete" value="Delete" onClick="setDeleteAction();" />
<input type="button" name="assign" id="assign" value="Assign" onClick="setLeadAssignAction();" />
<?php
$sql = mysql_query("SELECT *FROM login where role=1");
while ($row = mysql_fetch_array($sql)){
?>
<tr class="listheader">
<td><input type="checkbox" name="eid[]" value="<?php echo $row["eid"]; ?>" ><?php echo $row["username"]; ?></td>
</tr>
<?php
}
?>
</td>
</tr>
</table>
</div>
</form>
<form class="form" method ="Post"action="" id="contact">
<?php
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
for($i=0;$i<$rowCount;$i++) {
$result = mysql_query("SELECT * FROM clientreg WHERE Id='" . $_POST["users"][$i] . "'");
$row[$i]= mysql_fetch_array($result);
echo "shakti";
echo $row[$i]['id'];
}
}
?>
<img src="button_cancel.png" class="img" id="cancel"/>
<div id="left" style="height:400px;width:47%;float:left;margin-left:20px;margin-top:15px;border-radius:10px;">
<label>Lead Owner: <span>*</span></label>
<br/>
<input type="text" name="leadowner[]" id="lead" placeholder="Lead Owner"value=""/><br/>
<br/>
<label>First Name: <span>*</span></label>
<br/>
<input type="text" name="fname"id="fname" placeholder="Fname"/><br/>
<br/>
<label>Last Name: <span>*</span></label>
<br/>
<input type="text" name="lname" id="lname" placeholder="Lname"/><br/>
<br/>
<label>Mobile No: <span>*</span></label>
<br/>
<input type="text" name="mobile"id="mobile" placeholder="Mobile"/><br/>
<br/>
<label>Email Id: <span>*</span></label>
<br/>
<input type="text"name="email" id="email" placeholder="Email"/><br/>
</div>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Where am I wrong in this code?
Please sort out my problem
My problem is here
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
Any help will be appreciated
I have updated my code and added submit button.
You have two forms. Only the second form has a submit.
Form 1:
<form name="frmUser" method="post" action="">
<input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?> ">
</form>
Form 2:
<form class="form" method ="Post"action="" id="contact">
<input type="submit" name="submit" value="Submit">
</form>
The if construct does not receive the $_POST["users"], because it receives only the POST of the second submitted form.
$rowCount = count($_POST["users"]);
$rowCount will always be 0.
Form 2:
...
<?php
$rowCount = 0;
if ($_POST["users"] != "") {
$rowCount = count($_POST["users"]);
} else if ($_POST["rowcount"] != "") {
$rowCount = $_POST["rowcount"];
}
?>
...
<form class="form" method ="Post"action="" id="contact">
...
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="rowcount" value="<?php echo $rowCount; ?>">
</form>
Then the variable $rowCount will contain the count of the rows after the submit of any of the 2 forms.

two submit button in a page on clicking a radio button in php

I have a page where there are two radio button on clicking one it shows one div and on clicking another radio button it shows another div.
In both div i have to insert the data into the database but only fist submit button is working the second button on second div is not working.Below is my code:
<body>
<center>
<h2>Choose the type </h2>
<div style="padding:25px;width: 100px;">
<input id="id_radio1" type="radio" name="name1" value="1" />Dynamic<br/>
<input id="id_radio2" type="radio" name="name0" value="0" />Image
</div>
<div align="center" style="padding:25px;width: 300px;">
<div id="div1">
<form name="frm" method="post" action="">
Caption:<input type="text" name="caption" /><br />
Dynamic:<input type="text" name="dynamictext" /><br/>
Time <input type="text" name="time" /><br />
<input type="submit" name="submitbtn1" value="Submit" />
</form>
</div>
<div id="div2">
<form enctype="multipart/form-data" action="saveimage.php" method="post" name="changer">
Path:<input name="image" accept="image/jpeg" type="file" height="50px" width="50px"><br/>
Caption:<input type="text" name="caption" />
Time:<input type="text" name="caption" />
<input type="submit" name="submitbtn0" value="Save" />
</form>
</div>
</div>
</center>
</body>
</html>
<?php
if(isset($_POST['submitbtn1']))
{
include'conn.php';
$select_db = mysql_select_db('test',$connection);
//$type1=$_POST['name1'];
$sql="INSERT INTO tbl_content(caption,dynamictext,time)VALUES('".$_POST['caption']."', '".$_POST['dynamictext']."', '".$_POST['time']."')";
if (!mysql_query($sql,$connection))
{
die('Error: ' . mysql_error());
}
}
else if(isset($_POST['submitbtn0']))
{
include'conn.php';
$select_db = mysql_select_db('test',$connection);
if ($_FILES["image"]["error"] > 0)
{
echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],"image/" . $_FILES["image"]["name"]);
$file="image/".$_FILES["image"]["name"];
$sql="INSERT INTO tbl_content (id,path,caption,time) VALUES ('','$file','".$_POST['caption']."','".$_POST['time']."')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "Picture is saved into database";
}
}
?>
I think your saveimage.php file seeking a value for $_POST['time'] but second form has no value for that. Because your second form has Time:<input type="text" name="caption" /> and there is another input for that name too.
So change
Time:<input type="text" name="caption" />
to
Time:<input type="text" name="time" />
in your second form and you may success.
also sanitize your data before enter these data to database. I think best way is use prepared statements with mysqli.

Keep form data inside the field after submission using php

I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />

Not validating one of the search fields

I want to check if either of my search fields are empty, and if they are i write out a failure message. For some reason the following code doesn't work when I try to submit empty values in both search fields, or leaving the q2 search field empty. It only writes out the failure message when I only insert a value into the first field. Hope you guys can help.
<div id="searchField">
<form name="searchField" action="" method="get">
<input type="search" name="q" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="search" name="q2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>
<input type="hidden" id="v" name="v" value="2" />
<input type="hidden" id="b" name="b" value="" />
<button type="submit" id="loop" style="border:0px; background:transparent; vertical-align:middle; margin-left:-85px; margin-top:-3px;">
<img src="images/loop.png" width="75" height="75" alt="submit" />
</button>
</form>
</div>
<div id="theData">
<?
if(str_replace(" ", "",$_GET['q'])!="" && str_replace(" ","",$_GET['q2'])!=""){
$qContent = $_GET['q'];
$succes = mysql_query("SELECT*FROM film WHERE name='".$qContent."'");
include("searchMaster.php");
if(mysql_num_rows($succes)) {
while($o = mysql_fetch_array($succes)){
echo $o['name'];
if($o['trailer']){
echo '<div id="ytplayerWrap">';
echo '<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/'.$o['trailer'].'?http://amnestic.ueuo.com"
frameborder="0"/>';
echo '</div>';
}else {
echo '<span style="font-size:20px;">';
echo '<br /><br /> No trailer was found for <em>'.$_GET['q'].'</em> :(';
echo '</span>';
}
}
}else{
echo 'You did the fuckup :(';
}
}else{
echo 'You did the fuckup :(';
}
?>
The name and id of your input fields must be same
Like this
<input type="text" name="submitQuery" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="text" name="submitQuery2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>

Categories