I have a form that has two field inputs that also have the same name "name"
I was wondering what do I have to do , to capture both fields in array,
for example, if field1 get post and field2 was empty, I would like to get an array[0]
with the data I was looking for and if field1 and field2 where submited, I would like to have array[0] and array[1] populated.
currently the form I have will only capture if both fields are populated and if one of them is populated it wonn't capture .
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
$name=$_POST['name'];
if($name)
{
echo $name;
}
?>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<p>name
<input name="name" id="name" type="text" />
</p>
Name
<input name="name" id="name" type="text" />
<p> </p>
<input name="submit" type="submit" />
</form>
</body>
</html>
I would like the end results for the variable name to be an array that would have both data that was capture from field one and field two.
thanks
Change the name of each field to name[], then they can be accessed in PHP using $_POST['name'][0] and $_POST['name'][1].
EDIT: You can also use name[1] in your HTML to explicitly set indices, or even name[foo], then access from PHP using $_POST['name']['foo'].
Related
how to input a number range, from 2 text fields, this is my code is seems to be not working, the range function i want it to be inserted to mysql database but this code is just to test if i can get the 2 textfields to connect to the range function. anyone can help me solve this problem?
<?php
$from = '.from';
$to = '.to';
$number = range('$from','$to');
print_r ($number);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Waybill</title>
<link rel="shortcut icon" href="../img/icon.ico">
<link rel="stylesheet" type="text/css" href="../css/forms.css" />
</head>
<body>
<form id="form3" name="form1" method="post" action="">
<p>From:
<input type="text" name="from" id="form_number" class="from" value="0"/>
- To:
<input type="text" name="to" id="form_number" class="to" value="50" />
</p>
<p>Waybill Booklet:
<select name="waybill_booklet" id="form_list">
</select>
</p>
<p>
<input type="submit" name="button" id="form_button" value="OK!" />
</p>
</form>
</body>
</html>
Replace your php code with this:
<?php
if (!empty($_POST)) {
$from = $_POST['from'];
$to = $_POST['to'];
$number = range($from,$to);
print_r($number);
}
?>
To access a form post in PHP you use the $_POST superglobal. More info found here: http://www.php.net/manual/en/reserved.variables.post.php.
I've put a check to see if it's empty or not at the start, so when the page first loads it won't attempt to run the PHP code. Presumably you'll then replace the print_r with whatever you need to insert the data into your database.
<?php
session_start();
$_SESSION['id']='face';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="check.php" method="post">
Admin: <input type="text" name="uname" value="<?php if(isset($_SESSION['id']) && !empty($_SESSION['id'])) { echo $_SESSION['id']; } ?>" /><br />
Password: <input type="password" name="pword" /><br />
<input type="hidden" name="login" value="1" />
<input type="submit" value="Login" />
</form>
</body>
</html>
I am working in a php language . i have made session[id] in which i have stored the the value ie face . Now in the form , ADMIN textbox , i am checking for the session[id] , if isset or not empty *echo* session[id] but if not isset or empty echo the textbox value instead of this its showing me tha number . please can any one explain me why its showing me the number .......instead of value
First, correct your html as follows:
<input type="text" name="uname" value="<?php if(isset($_SESSION['id']) && !empty($_SESSION['id'])) { echo $_SESSION['id']; } ?>" />
if you get the same error you need to check your session value before displaying the form. You can use:
var_dump($_SESSION);
to make sure if your session is not overwritten somewhere else.
There is no <input type="textbox" in html!
Use <input type="text" or <textarea> </textarea> (content goes in between)
EDIT: Session ID
If you really want to overwrite the Session ID, use the function session_id(sid). Either way, you can only set the session ID before you start the session, so your code should look like this:
session_id("face");
session_start();
If i give textbox type as number and submit a form it cut off 0 .
ex: 099921 means it will post as 99921
why it cut off preceding 0
Because 0 in front means nothing and is removed. Make it a string (text) and it will stay. Ints have no 0 in front.
i dont know why but i did the same code and it is displaying the value with o i.e 099921
check out my code
<?php
if(isset($_POST['submit']))
{
echo $numberValue = $_POST['numVal'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post">
<input type="number" name="numVal" value="" />
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php $_POST array empty upon form submission
I just make a simple test to POST a HTML format's textfield data to PHP server. And, in PHP server side, just use the $_POST[] function to retrive the posted data.
However, there is empty(null) data recevied always. Attached below are the HTML code and php script I tested.
Appreaicted if you can point me where I was wrong?
----HTML code---
senddata.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>Untitled Document</title>
</head>
<body>
<form action="receivedata.php" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
<label>_id
<input name="_id" type="text" id="_id" />
</label>
<br />
<label>Send
<input type="submit" name="Submit" value="Send" />
</label>
</form>
</body>
</html>
---PHP Script-----
receivedata.php
<?php
echo $_SERVER['SERVER_NAME']."<BR/>" ;
$id = $_post['_id'];
if ( isset($id))
{
echo "Data set ready!<br/>";
echo "id=".$id."<br/>";
}
else
{
echo "Data set is not ready!<br>";
}
?>
and the result display on the browser:
192.168.0.108
Data set is not ready!
$_POST[] is not a function but language construct used to add items into $_POST array.
While $_POST is a variable, though you are not using it anyway but whatever $_post variable instead.
I have an HTML form with three fields, first name surname and mid. name and I want to save this information in a php page. The problem is I don't know how to save multiple entries in array without a connection of database.
HTML form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Εργασία 11 - Βασίλης Τρίγκας</title>
</head>
<body>
<h4>Καταχώριση βιβλίου</h4>
<form action="form_action.php" method="get">
Συγγραφέας <br/><input type="text" name="fnwriter" /><br/>
Τίτλος <br/><input type="text" name="title" /><br/>
Εκδότης<br/> <input type="text" name="editor" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP File
<?php
function display_welcome(){
print("Σωστή εισαγωγή, ");
print($_GET['fnwriter','title','editor'])
}
function display_empty_form(){
print <<<_HTML_
<FORM method="post" action="$SERVER['PHP_SELF']">
<BR/>
<INPUT type="submit" value="SUBMIT NAME">
</FORM>
_HTML_;
}
if ($_GET['fnwriter','title']) {
display_welcome();
}
else {
display_empty_form();
}
$Book=array("fnwriter","title","editor");
$fwriter=$_GET['$fnwriter']
$title=$_GET['$title']
$editor=$_GET['$editor']
echo $fnwriter;
echo $title;
echo $editor;
for ($i = 0; $i < count($fnwriter,$title,$editor); ++$i) {
echo "Book $i= $fnwriter[$i],$title[$i],$editor[$i]<br />";
}
//echo $Book[$_GET];
//print_r($);
?>
I don't really get your question, is that about saving data to a file, or about processing $_POST data?
If it's the first one, you have a plenty of possibilities, from simple CSV (or any other separator) to XML ;)