I have a form on a page like:
<form action='search.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='submit' />
</form>
on search page there is another form like
<form action='' method='POST'>
<input type='submit' name='anygender' />
</form>
then i am using
if(isset($_POST['anygender'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}
then $specialist is showing blank and sql query not working..I want to use both form's post value together.Please tell me how to use first form post value in this. Thanks in advance
Why you need two forms?
<form action='index.php' method='POST'>
<input type='text' name='specialist' />
<input type='submit' name='anygender' />
<input type='submit' name='submit' />
</form>
Maybe you can use one form and check buttom?
Use this code. Check $_POST['submit'] in if condition.
if(isset($_POST['submit'])){
$speciality = $_POST['speciality'];
echo $speciality;
$a = mysql_query("SELECT * FROM find_doctor WHERE doctor_type LIKE '%$speciality%'");
while ($b = mysql_fetch_array($a)){
echo "<img src='$b[image]' height='150px' width='300px'>"."</br>";
echo $b['name']."</br>";
echo $b['doctor_type']."</br>";
echo $b['location']."</br>";
echo $b['insurance']."</br>";
echo $b['comments']."</br>";
echo $b['address']."</br>";
}
}
Related
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a>  ";
echo "<input type='submit' name='acc' value='Accept'>   <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a>  ";
echo "<input type='submit' name='acc' value='Accept'>   <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo
I have the following code:
<form action='' method='POST' id='form1'>
imdbcode : <input type='text' id='imdbcode' name='imdbcode' /><br/>
<input type='submit' name='submit' value='Get'/>
</form>
<?php
if(isset($_POST['submit'])){
.
.
.
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
//I WANT TO USE $Title from condition1
} ?>
I want to use $title in second condition.
It prints $title in first condition! But doesn't print after closing condition!
How can I do that?
You can't.
Before you can enter the if(isset($_POST['Send']))
you need submit this one:
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
but the moment you submit this, the page will be refreshed and $_POST['submit'] will be deleted, without this variable the $title will not exist.
to fix this or make the $title value alive until the next refresh of the page. you must include the $title value along with the form2.
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value="$title"/>
<input type='submit' name='Send' value='Send'/>
</form>";
and when the user send that form2 you can access the title by using
$_POST['title']
As two your forms are different - there's no connection between them unless you explicitly provide it.
Simple solution can be just echo your $title as a hidden field in a form:
if(isset($_POST['submit'])){
$title = ... ;
echo "
<form action='' method='POST' id='form2'>
<input type='hidden' name='title' value='" . $title . "'/>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_POST['title'];
// do other stuff
}
Another solution is to use sessions:
if(isset($_POST['submit'])){
$title = ... ;
$_SESSION['title'] = $title;
echo "
<form action='' method='POST' id='form2'>
<input type='submit' name='Send' value='Send'/>
</form>";
}
if(isset($_POST['Send'])){
echo $_SESSION['title'];
// do other stuff
}
In case of using sessions don't forget to use session_start and probably to unset $_SESSION['title'] in the end of second submit.
<form action='' method='POST' id='form2'>
<input type='hidden' name='text' value='<?php echo $title; ?>
<input type='submit' name='Send' value='Send'/>
</form>
Try to add a hidden input before the Send button.
i'm working on some code it seems im stuck... anyone can fix the right source code for looping array form?
<pre>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<form action='' method='POST'>
<?php
if(isset($_POST['jml'])){
$jml = $_POST['jml'];
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name"> 
Stuff Price <input type='text' name='price'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}
if(isset($_POST['submit2'])){
$name[] = $_POST['name'];
$price[] = $_POST['price'];
global $jml;
for($i=0;$i<$jml;++$i){
echo $name.' '.$price.'<br>';
}
}
?>
</pre>
i was basic at C++ it's so easy to loop, but i'm still much to learn on this php so anyone can help me please?
What you need to do first is if you're expecting multiple row fields, put a [] in your name attribute in text fields. Example:
<input type="text" name="name[]">
This in turn will accept multiple text inputs under the same name turning it into an array.
Next, since you're reliant on the number of fields to be generated by $jml = $_POST['jml']; this will just be available on the first request. On the next submission this will be gone. Instead of using that with a global which doesn't make sense, just use the count of the submitted text fields.
$name = $_POST['name'];
$price = $_POST['price'];
$count = count($name); // get count
Take not that this is reliant to the forms all fields being complete.
After that, its just basic array pointing. echo $name[$i]:
Revised code:
<pre>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<form action='' method='POST'>
<?php
if(isset($_POST['jml'])){
$jml = $_POST['jml'];
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name[]"> 
Stuff Price <input type='text' name='price[]'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}
if(isset($_POST['submit2'])){
$name = $_POST['name'];
$price = $_POST['price'];
$count = count($name);
for($i=0;$i<$count;++$i){
echo $name[$i].' '.$price[$i].'<br>';
}
}
?>
</pre>
Use foreach loop This is how you can easily loop through an array:
foreach($_POST as $key=>$value){
echo $value;
}
Check this
Working code here http://main.xfiddle.com/7ffb488b/stackoverflow/Loopanarraywithform.php
<pre>
<?php
if(isset($_POST['submit'])){
echo "<form action='' method='POST'>";
$jml = intval($_POST['jml']);
for($i=0;$i<$jml;++$i){
?>
Stuff Name <input type="text" name="name[]"> 
Stuff Price <input type='text' name='price[]'><br>
<br>
<?php
}
echo "<input type='submit' name='submit2'>";
echo "</form>";
}elseif(isset($_POST['submit2'])){
$name = $_POST['name'];
$price = $_POST['price'];
// global $jml;
$i=0;
foreach ($name as $key => $value) {
echo $value.' '.$price[$key].'<br/>';
$i++;
}
}else{?>
<form action='' method='POST'>
<input type='text' name='jml'>
<input type='submit' name='submit'>
</form>
<?php
}
?>
</pre>
So basically I have the user enter a number on my first screen.
Here is test1.php which generates the number of text boxes that the user had previously entered. That number is $input
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='Names'>";
}
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
Then my test2.php should print all the values entered, but it only prints out the last value entered from test1.php. For example is $input is 4, only the text entered in the 4th text box prints, which is understandable as I don't know how to print all values.
$names=$_POST['Names'];
foreach($number as $num){
echo $num;
}
Is the problem with the name I gave to the textboxes, or something else?
Any help is much appreciated.
Just create a name grouping attribute, so that you'll get an array of inputs instead of just one:
<input type='text' name='Names[]'>
// ^ this is important
Sidenote:
I don't know if this is a typo, but this should be $names instead of $number:
$names = $_POST['Names'];
foreach($names as $num){
echo $num . '<br/>';
}
Sample Demo
Your problem is that you give the same name to all of your input (in your test1.php) so when you try to restore them on your test2.php, your $_POST['Names'] just takes the last input with this name.
A solution is to give a different name to all of your input
In yout first file use it :
echo "<form action='test2.php' method='post'>";
for($i=1; $i<=$input; $i++)
{
echo "Entry $i";
echo "<input type='text' name='".$i."'>";
}
echo "<input type='hidden' name='input' value='".$input."'>";
echo "<input type='submit' class='button' name='submit' value='submit'>";
echo "</form>";
And in your 2nd file :
for($i=1; $i<=$_POST['input']; $i++){
echo $_POST['$i'];
}
<form method="post" name="myform">
<input type="text" name="array[]" Value="101"/>
<input type="text" name="array[]" Value="102"/>
<input type="text" name="array[]" Value="103"/>
<input type="text" name="array[]" Value="104"/>
<input type="submit" name="submit" Value="submit"/>
</form>
if(isset($_POST['submit'])){
foreach($_POST['array'] as $myarray) {
echo $myarray.'<br>';
}
OUTPUT
101
102
103
104
I have this and works fine. But can someone tell me is there a SMARTER way to validate this?
In the validation here, you may find that the echo "<form....... is replicated thrice to make sure that the form content is also visible along side the validation message when the form is submitted. Im sure there is a more refined method of validating text box content without form being replicated multiple times.
Also appreciate if some one can suggest me a method to retain the text box value after the form is being submitted. Usually when the form is submitted the value you enter disappears.
Thanks
<?php
include ("../connection/index.php");
?>
<form method='post' action=''>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
<?php
if(isset($_POST['sendone']))
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
}
if(isset($_POST['sendtwo']))
{if($_POST['txt1']=='')
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
echo "Empty";}
else
{
echo "Hello";
echo "<form method='post' action=''><input type='text' name='txt1' id='txt1'><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></form>";
echo "Hit success!";
}}
?>
EDIT
Concept
<hard coded submit1 button>
<dynamically create a text box with a submit2 button>
<when submit2 is activated is should validate the text box content>
When the validation happens both the vaidated message and the text box should be visible
did you mean something like this?
<?php
if(isset($_POST['sendtwo']))
{
echo "Hello";
}
?>
<form method='post' action=''>
<?php
if (!isset($_POST['sendone'])) {
?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
<?
} else {
?>
<input type="hidden" name="sendone" id="sendone" value="OneClick">
<input type='text' name='txt1' id='txt1'<?=isset($_POST['txt1']) && trim($_POST['txt1']) !== '' ? ' value="'.trim($_POST['txt1']).'"' : ''?>>
<input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>
<?
}
?>
</form>
<?php
if (isset($_POST['sendtwo'])) {
if (trim($_POST['txt1']) == '') {
echo 'Empty';
} else {
echo 'Hit success!';
}
}