In php, if you name your form fields using numerical indexes, they work as arrays in the $_POST object.
<form method="post" action="post.php">
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST, JSON_NUMERIC_CHECK);
}
?></p>
</form>
outputs
{"question":[{"name":"a","email":"aa","password":"aaa"},{"name":"b","email":"bb","password":"bbb"}]}
If the ordering of the fields is not sequential starting at zero and incrementing by only one each time the name is repeated, then they are all interpreted as keys instead. So
<form method="post" action="post.php">
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST, JSON_NUMERIC_CHECK);
}
?></p>
</form>
outputs
{"question":{"1":{"name":"a","email":"aa","password":"aaa"},"0":{"name":"b","email":"bb","password":"bbb"}}}
Is there a way to get $_POST to ignore the order of arrays of post keys so they are interpreted as an array?
Please check if helpful or not :
<form method="post" action="#">
<input type="text" name="question[1][name]" />
<input type="text" name="question[1][email]"/>
<input type="text" name="question[1][password]" />
<hr>
<input type="text" name="question[0][name]" />
<input type="text" name="question[0][email]"/>
<input type="text" name="question[0][password]" />
<hr>
<input type="submit" value="Add" />
<hr>
<p>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
ksort($_POST['question']);
print_r($_POST['question']);
}
?>
</p>
</form>
Related
In the code below only the values from the last two forms are displayed. What could I be doing wrong?
<form action="" method="post">
<input name="fields[edu][name][]" />
<input name="fields[edu][age][]"/>
<br/><br/>
<input name="fields[edu][name][]" />
<input name="fields[edu][age][]"/>
<br/><br/>
<input name="fields[edu][name][]" />
<input name="fields[edu][age][]"/>
<br/><br/>
<input type="submit" name="submit" value="Submit"/>
if(isset($_POST['submit'])){
print_r($_POST['fields']);
foreach($_POST['fields'] as $field){
echo '<br/>';
echo 'Hello your name is : '.$field['name'];
echo '<br/>';
echo 'Hello your age is : '.$field['age'];
}
}
Update: What would be the best way to echo the values from the array? I've tried several methods none works
<form action="" method="post">
<input name="fields[edu0][name][]" />
<input name="fields[edu0][age][]"/>
<br/><br/>
<input name="fields[edu1][name][]" />
<input name="fields[edu1][age][]"/>
<br/><br/>
<input name="fields[edu2][name][]" />
<input name="fields[edu2][age][]"/>
<br/><br/>
<input type="submit" name="submit" value="Submit"/>
Here is my existing code:
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
This simply posts any of the forms which are submitted, individually.
What I'm trying to accomplish is submitting these individual forms to a specific table in the same database.
So for example, Form1 would be submitted to Table1, Form2 to Table2, etc. Each form will always be submitted to it's matching table.
Change the name foreach of your Submit input form element, for example to submitForm1, submitForm2 and submitForm3, like:
<input type="Submit" value="Submit Form" name="submitForm1" />
<input type="Submit" value="Submit Form" name="submitForm2" />
<input type="Submit" value="Submit Form" name="submitForm3" />
Then in your php logic you could do something like:
if(isset($_POST['submitForm1'])){
// Do things with your form1
}elseif(isset($_POST['submitForm2'])){
// Do things with your form2
}elseif(isset($_POST['submitForm3'])){
// Do things with your form3
}
the $_POST or $_GET array is composite of your input tag. So when you call $_POST['submitForm'] you should have an input with that name like
<input name ='sumbitform' />
If you need to get separate form just change your code in this way:
<?php
if (isset($_POST['submitForm1'])) {
//sql statement for table 1 there
}
if (isset($_POST['submitForm2'])) {
//sql statement for table 2 there
}
if (isset($_POST['submitForm3'])) {
//sql statement for table 3 there
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>
In this
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
After I submit the form, the page refreshes and the data inside the input texts gets blank
Is it possible to keep the data even after submit?
Regards.
You can simply use ajax for submitting the form.
Or use following
<form method="POST" action=""><input type="text" class="field small-field" name="tex1" value="<?php (isset($_POST['text1]))? echo $_POST['text1] : '';" /><input type="submit" value="search" name="search"/><input type="submit" value="print" name="print"/></form>
try to echo, what ever is the variable named for your input.
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1'];?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
With php for example:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php echo $_POST['tex1']; ?>"/>
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
If you are handling the post on the same page you could just do like this on the fields where you want the posted value to be shown:
<input type="submit" value="search" name="search" <?php if( isset( $_POST['search'] ) ){ echo "value=\"". $_POST['search'] ."\"; } ?>/>
Use this:
<form method="POST" action="">
<input type="text" class="field small-field" name="tex1" value="<?php if(isset($_POST['tex1'])) echo $_POST['tex1'] ?>" />
<input type="submit" value="search" name="search"/>
<input type="submit" value="print" name="print"/>
</form>
Bascially http is statelessprotocol , hence you need to save the data some where
The simplest way in this case would be to use a conditional operator
<input type="text" class="field small-field" name="tex1" value="<?php echo (isset($_POST['search'] || $_POST['search'] )?$_POST['tex1']:''); ?>" />
What I'm trying to do here is display my login information and once logged in display something else
Example is here
<?php
$user = JFactory::getUser();
$status = $user->guest;
if($status == 1){
echo '
<form id="login-form" class="form-inline" method="post" action="/home">
<div class="topUser">
Name:
<input id="modlgn-username" class="TopinputReg" type="text" placeholder="User Name" tabindex="0" name="username" />
</div>
<div class="toppass">Password:
<input id="modlgn-passwd" class="TopinputReg" type="password" placeholder="Password" tabindex="0" name="password" pwfprops="," />
</div>
<div class="topsignin">
<input class="Signin" name="Submit" tabindex="0" type="submit" value="" />
</div>
<div class="topregister">
<input name="button" type="submit" class="Register" id="button" value="" />
</div>
<input type="hidden" value="com_users" name="option">
<input type="hidden" value="user.login" name="task">
<input type="hidden" value="aW5kZXgucGhwP0l0ZW1pZD0xMzM=" name="return">
<input type="hidden" value="1" name="39ea5446d876078c6f6221d396ef5bd4">
</form>
';
}
else
{
echo '
<div class="topUser">Welcome Back!</div>
<div class="toppass">Enjoy you stay.</div>
<form id="login-form" class="form-vertical" method="post" action="/log-out">
<div class="topsignin">
<input class="Signout" type="submit" value="" name="Submit" />
<input type="hidden" value="com_users" name="option">
</div>
<input type="hidden" value="com_users" name="option">
<input type="hidden" value="user.logout" name="task">
<input type="hidden" value="aW5kZXgucGhwP0l0ZW1pZD0xMDE=" name="return">
<input type="hidden" value="1" name="994c61f8ab4ccf23fe9dae6546a87089">
</form>
<div class="topregister">
<input name="button" type="submit" class="account" id="button" value="" />
</div>
';
}
?>
No I know what I am doing wrong just have no clue how to fix it. the two
need to have a different generated number each time but how do i do this.
since this numbers are the same every time if i log in then out then try to log back in it will give me "Invalid Token"
When i go into the login template it gives me
this for login
<input type="hidden" name="return" value="<?php echo base64_encode($this->params->get('login_redirect_url', $this->form->getValue('return'))); ?>" />
<?php echo JHtml::_('form.token'); ?>
and this for log out
<input type="hidden" name="return" value="<?php echo base64_encode($this->params->get('logout_redirect_url', $this->form->getValue('return'))); ?>" />
<?php echo JHtml::_('form.token'); ?>
Now if I replace the hidden fields that already have the pregenerated numbers with the php stuff above when i try to refresh my page it goes completely blank.
I am at a loss with this see as how I am not very advanced in php. any help anyone cant provide would be massively helpful..
why not try like this,
<?php
$user = JFactory::getUser();
$status = $user->guest;
if($status == 1):
?>
<form id="login-form" class="form-inline" method="post" action="/home">
<div class="topUser">
Name:
<input id="modlgn-username" class="TopinputReg" type="text" placeholder="User Name" tabindex="0" name="username" />
</div>
<div class="toppass">Password:
<input id="modlgn-passwd" class="TopinputReg" type="password" placeholder="Password" tabindex="0" name="password" pwfprops="," />
</div>
<div class="topsignin">
<input class="Signin" name="Submit" tabindex="0" type="submit" value="" />
</div>
<div class="topregister">
<input name="button" type="submit" class="Register" id="button" value="" />
</div>
<input type="hidden" value="com_users" name="option">
<input type="hidden" value="user.login" name="task">
<input type="hidden" name="return" value="<?php echo base64_encode($this->params->get('login_redirect_url', $this->form->getValue('return'))); ?>" />
<?php echo JHtml::_('form.token'); ?>
</form>
<?php else:
?>
<div class="topUser">Welcome Back!</div>
<div class="toppass">Enjoy you stay.</div>
<form id="login-form" class="form-vertical" method="post" action="/log-out">
<div class="topsignin">
<input class="Signout" type="submit" value="" name="Submit" />
<input type="hidden" value="com_users" name="option">
</div>
<input type="hidden" value="com_users" name="option">
<input type="hidden" value="user.logout" name="task">
<input type="hidden" name="return" value="<?php echo base64_encode($this->params->get('logout_redirect_url', $this->form->getValue('return'))); ?>" />
<?php echo JHtml::_('form.token'); ?>
</form>
<div class="topregister">
<input name="button" type="submit" class="account" id="button" value="" />
</div>
<?php endif;?>
I have 2 text fields, user and password. When I enter the password, it jumps to the first one. I have no idea why that is happening. I couldn't find why it is jumping. How do I change it so the password form doesn't jump? Here is the code:
<?php
session_start();
require_once 'database.php';
if (isset($_SESSION['user'])){
echo "Welcome ".$_SESSION['user'];
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
<br /><form name="news" method="post" action="news.php">
<input type="submit" name="news" id="news" value="News">
</form>
<?php
}
elseif(isset($_SESSION['admin'])){
echo"Welcome ".$_SESSION['admin'];
echo"<br><br>You are logged in as an Admin";
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
</form>
<?php
}else{
?>
<form name="login_form" method="post" action="login2.php">
<label>
<input name="user" type="text" id="user">ID<br />
<input name="pass" type="password" id="pass">Password<br />
</label>
<input type="submit" name="login" id="login" action="index.php" value="Login">
</label>
</p>
</form>
<form name="Register" method="post" action="reg.php">
<input type="submit" name="register" id="register" value="Register">
</form><br />
<form name="news" method="post" action="news.php">
<input type="submit" name="news" id="news" value="News">
</form>
<?php
}
?>
You have both of the inputs wrapped in one label. The browser is getting confused and it thinks that that entire content is a label for the first input (that's how labels work, apparently). You should only use <label> to wrap text for input.
This:
<label>
<input name="user" type="text" id="user">ID<br />
<input name="pass" type="password" id="pass">Password<br />
</label>
Should be this:
<input name="user" type="text" id="user">
<label for="user">ID</label><br />
<input name="pass" type="password" id="pass">
<label for="pass">Password</label><br />