Create custom array of input name values - php

If I have two inputs like the following
<input type="text" name="id">
<input type="text" name="name">
Then it is possible to get values in array on backend like this
array("id"=>"name")
If it is possible then how it can be done?

Hope you are doing well and good.
Umm, I just got your query. You want to make id as a key and name as a value in array. So here, May be i found your solution.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<form method="post">
<input type="text" name="id[]">
<input type="text" name="name[]">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$combineArr = array_combine($_POST['id'], $_POST['name']);
print_r($combineArr);
}
?>
From the above code you can get answer like below,
Array ( [id] => name )

You can send your post via POST method.
<form name="form" action="" method="post">
<input type="text" name="id">
<input type="text" name="name">
<input type="submit" name="submit_button"
value="Send"/>
</form>
After that :
<?php
$array = [$_POST['id'] => $_POST['name']];

Related

html form can not send data to [duplicate]

This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 2 years ago.
My html is not sending field data to php in same file until action and method attribute are placed.... kindly identify my error and guide me.....
<pre><code>
<?php
if(isset($_post['submit'])){
echo "Yes is is working";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>testing form</title>
</head>
<body>
<br><br><br><br>
<form action="test.php" method="post">
<input type="text" name="name" placeholder="Enter Name"><br>
<input type="text" name="location" placeholder="ENter location"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
</pre></code>
Probably you could try by setting id instead of name.
Eg.
<form action="test.php" method="post">
<input type="text" id="name" placeholder="Enter Name"><br>
<input type="text" id="location" placeholder="ENter location"><br>
<input type="submit" id="submit">
</form>

html form with Php code not executing

I'm new to PHP. I have this html file with php in it:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Php torturail 1</title>
</head>
<body>
<p>php ahead:</p>
<?php
if (isset($_POST['submit'])){
printf('User Name: %s', $_POST['name']);
}
?>
<form method="post" action="">
<p>name:</p>
<input type="text" name="name">
<p>pass:</p>
<input type="password" name="pwd">
<p>massage:</p>
<textarea name="area"></textarea>
<p>accept:</p>
<input type="checkbox" name="chb" value="on">
<p>lucky number:</p>
<input type="radio" name="group1" value="option1">1
<input type="radio" name="group1" value="option2">2
<p>button:</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
When I open it on on a browser and click submit, the form's fileds are empty again but nothing is printed.
What is the problem?
Thank you.
2 things:
1-you must save your file as .php file.
2-fill action in your form tag
<form method="post" action="where.php">

multiple form with single submit button to retrieve value of fields within bith form

index.php
<html>
<head>
<script type="text/javascript">
function submitForms()
{
document.forms["form-1"].submit();
document.forms["form-2"].submit();
}
</script>
</head>
<body>
<form method="POST" action="form.php" id='form-1'>
<input type="text" name="txt1" />
</form>
<form method="POST" action="form.php" id='form-2'>
<input type="text" name="txt2" />
</form>
<input type="button" value="Click Me!" onclick="submitForms();" />
</body>
</html>
form.php
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Above is my code and when i submit both forms then both text-fields with their value it does not shoe me both text-field values.It only shoe me second text-field value.Please help me quickly.
I think because you try to get the params after sumbit two forms. You have sent the two forms at once and the second has stepped to the first, so the result is the return of the second form.
I think this will be better:
<html>
<head>
</head>
<body>
<form method="POST" action="form.php">
<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="submit" value="Click Me!" />
</form>
</body>
</html>
<?php
echo $_POST['txt1'];
echo $_POST['txt2'];
?>
Sorry for my english

html form - how to call different php pages based on the button selected

Problem: How to make an HTML Form call different php pages from the action based on what button is pushed?
The code below is the solution I have now, but I figure there must be a better way to do this then creating multiple forms on the page?
<html>
<body>
<form name="entry_form" action="entry_update_script.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="entry_id" value="">
<input type="hidden" name="entry_item_id" value="">
Truck/Railcar/Barge#:<input type="text" name="pro_number" value=""><br>
BOL #:<input type="text" name="bol" value=""><br>
<input type="submit" name="entry_submit" value="Add New Entry!">
</form>
<form name="entry_form_add" action="entry_view.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="entry_id" value="">
<input type="submit" name="submit" value="Add New Item!">
</form>
</body>
</html>
<html>
<body>
<script type="text/javascript">
function submitAction(act) {
document.sample.action = act;
document.sample.submit();
}
</script>
<form name ="sample" action="default.php">
<input type="button" value = "blah1" onClick="submitAction('phpPage1.php')">
<input type="button" value = "blah2" onClick="submitAction('phpPage2.php')">
</form>
</body>
</html>
you might choose the page to go from a dispatcher, it's an extensible and robust solution:
your form
<form action="dispatcher.php" method="POST">
<input type="radio" name="myOption" value="register" />
<input type="radio" name="myOption" value="login" />
</form>
dispatcher.php
$actions = array ('register', 'login');
// validate possible actions
if (in_array($_POST['myOption']), $actions)) {
include ($_POST['myOption'] . '.php');
}

Get response from form POST

I've looked all over the net for probably a common and simple task and have found nothing but deadends. I'm trying to grab a response from my own html page that uses POST to submit data to a website so I can parse it and show/print the parsed text on the same html page.
Here's what my html page looks like:
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action="http://somesite.com"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</head><body></body></html>
Just do <?php echo $_POST['username']; ?>
use the Predefined Variable $_POST
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action=""
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
<?php if("Send" == $_POST['submit']){ var_dump($_POST); } ?>
</head><body></body></html>
Would recommend that you read on http://www.php.net/form as it contains both examples and good comments. As your calling the fields username and password I guess you might also be looking at database connections, be very careful of SQL injections (http://php.net/manual/en/security.database.sql-injection.php).

Categories