I'm writing a form and this is its structure:
HTML:
<form method="post" action="script.php">
<input type="text" name="nombre" >
<input type="submit">
<input type="submit">
</form>
PHP:
<?php
if(isset($_POST))
$options = array();
$options[] = "";
// here I use the value input in "name" text input,
// so I need to get it from the form
for ($i=0;$i<=count($buscar)-1;$i++) {
$options[] = "<option value='{$i}'>{$dato}</option>";
}
echo '<select class="" id="articles" size="1" name="articles">';
echo implode("\n", $options);
echo '</select>';
?>
Is there any way to tell the first submit to let the php be executed (because it creates a select item)?
Then, when the select is selected, click on the second submit and send the complete form?
You can do in two ways:
Different Naming:
<input type="submit" name="sub1" />
<input type="submit" name="sub2" />
And you can check it using:
isset($_REQUEST["sub1"])
isset($_REQUEST["sub2"])
Passing Value:
<input type="submit" name="sub" value="Submit Here" />
<input type="submit" name="sub" value="Submit There" />
And you can check it using:
($_REQUEST["sub"] == "Submit Here")
($_REQUEST["sub"] == "Submit There")
Related
I have several links in an HTML page (say some 100 - 120). What I exactly need is to send different data to another PHP page (say display.php) when I click on each link. I tried a pathetic method,
<form action="display.php" method="post"> // or GET
<input type="hidden" name ="data" value="Data1" />
<input type="submit" value="Link 1" />
</form>`
.
.
.
<form action="display.php" method="post">
<input type="hidden" name ="data" value="Data120" />
<input type="submit" value="Link 120" />
</form>
Is there any simple way to reduce the usage of these 120 forms?
You can put data directly on the href of your tag.
Ex:
Click here
Then in your display.php file :
<?php
if(isset($_GET["data"]) && isset($_GET["data2"]))
{
$data = $_GET["data"];
$data2 = $_GET["data2"];
}
?>
Here you go.
yes sure
try to use tags with id's
link1
hope this helps
Try this
<form action='display.php' method='post'>
<input type="submit" value="Link 1" name='1' onclick='setHidden(this)'/>
<input type="submit" value="Link 2" name='2' onclick='setHidden(this)'/>
<input type="submit" value="Link 3" name='3' onclick='setHidden(this)'/>
<input type="submit" value="Link 4" name='4' onclick='setHidden(this)'/>
<input type="hidden" id='data' name ="data" value="" />
</form>
// Javascript
<script type='text/javascript'>
function setHidden( key)
{
var dataStr='Data';
dataStr+=key.name;
document.getElementById('data').value=dataStr;
}
</script>
then in display.php file
<?php
if(isset($_POST['data']))
{
$data=$_POST['data'];
echo $data;
}
?>
I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)
I have a database set up called customer. I also have a form that allows me to enter data into that database when submit, which is working fine.
However I also want to be able to search the database on the same page. I added a search field and button, and tried to use an if statement to determine which button was pressed.
The second button for searching the database doesn't seem to be doing anything when pressed, it doesn't seem to even enter the else if statement and I can't see why.
I have the following code:
<?php
require("header.php");
connect('final');//connect to DB
header ("location:/xampp/newCustomer.php");
if (isset($_POST['add'])) {
//do stuff for submit button here which works fine
}
else if (isset($_POST['btnSearch'])){
echo 'searching'; // test if button is working
$query = $_POST['searching'];
$data = mysql_query("SELECT *FROM customer WHERE 'First_Name' LIKE '$query'") ;
if($data === FALSE) {
$error = 'Query error:'.mysql_error();
echo $error;
}
else
{
$test = array();
$colNames = array();
while($results = mysql_fetch_assoc($data))
{
$test[] = $results;
}
$anymatches=mysql_num_rows($data);
if ($anymatches != 0)
{
$colNames = array_keys(reset($test));
}
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
}
}
}
?>
With my form setup like this:
<form name="add" action="newCustomer.php" method="post">
<label><span></span> <input type="text" name="query" palceholder="Type to Search" id="seaching"/></label>
<br />
<label><span>Name</span> <input type="text" name="addFname" /></label>
<button type="button" name="btnSearch" value="Search" id="btnSearch" onclick="this.form.action">Search</button></label>
<input type="hidden" name="searching" value="true" />
<input type="hidden" name="searching" value="true" />
<button type="submit" name="add" value="add" id="btnSub" >Add</button></label>
</form>
</html>
you use type="button", that gives it a button structure but a button dont sumbit a form only a submit does that.
<form name="add" action="newCustomer.php" method="post">
<label><span></span> <input type="text" name="query" palceholder="Type to Search" id="seaching"/></label>
<br />
<label><span>Name</span> <input type="text" name="addFname" /></label>
<button type="button" name="btnSearch" value="Search" id="btnSearch" onclick="this.form.action">Search</button></label>
<input type="hidden" name="searching" value="true" />
<input type="hidden" name="searching" value="true" />
<button type="submit" name="add" value="add" id="btnSub" >Add</button></label>
</form>
</html>
Try this:
HTML:
<form action="NewCustomer.php" method="post">
<table>
<tr>
<td> Name: </td>
<td><input type="text" name="name"></td>
<td><input type="submit" name="add" value="add"></td>
</tr>
</form>
<form action="NewCustomer.php" method="post">
<tr>
<td>Search: </td>
<td><input type="text" name="search"></td>
<td><input type="submit" name="search" value="search"></td>
</tr>
</table>
</form>
And PHP:
<?php
if (isset($_POST['add'])) {
echo "add";
}
if (isset($_POST['search'])){
echo "search";
}
?>
I think this is what you need :)
I have several links in an HTML page (say some 100 - 120). What I exactly need is to send different data to another PHP page (say display.php) when I click on each link. I tried a pathetic method,
<form action="display.php" method="post"> // or GET
<input type="hidden" name ="data" value="Data1" />
<input type="submit" value="Link 1" />
</form>`
.
.
.
<form action="display.php" method="post">
<input type="hidden" name ="data" value="Data120" />
<input type="submit" value="Link 120" />
</form>
Is there any simple way to reduce the usage of these 120 forms?
You can put data directly on the href of your tag.
Ex:
Click here
Then in your display.php file :
<?php
if(isset($_GET["data"]) && isset($_GET["data2"]))
{
$data = $_GET["data"];
$data2 = $_GET["data2"];
}
?>
Here you go.
yes sure
try to use tags with id's
link1
hope this helps
Try this
<form action='display.php' method='post'>
<input type="submit" value="Link 1" name='1' onclick='setHidden(this)'/>
<input type="submit" value="Link 2" name='2' onclick='setHidden(this)'/>
<input type="submit" value="Link 3" name='3' onclick='setHidden(this)'/>
<input type="submit" value="Link 4" name='4' onclick='setHidden(this)'/>
<input type="hidden" id='data' name ="data" value="" />
</form>
// Javascript
<script type='text/javascript'>
function setHidden( key)
{
var dataStr='Data';
dataStr+=key.name;
document.getElementById('data').value=dataStr;
}
</script>
then in display.php file
<?php
if(isset($_POST['data']))
{
$data=$_POST['data'];
echo $data;
}
?>
I would like the following:
Data = "123";
if checkbox clicked {Data="";}
Submit-button
That was the best way I could explain it.
I have tried, but I can't get the checkbox to overwrite the data with an empty value.
<form action="form.php" method="post">
<input type="text" value="" name="data" />
<input type="checkbox" value="checkbox" name="checkbox" />
<input type="submit" value="Submit" name="submit" />
</form>
That is your form, and this is your PHP
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['checkbox']))
{
$_POST['data'] = "";
}
/* the rest of your form */
}
?>