-1

Introduction

HTML page contains many input elements placed with submit button. Imagine that we don't know their elements' name or id.

Problem Statement

It is the requirement to retrieve available n number of text box values on click of submit button and without knowing their element names or ids. User entered values are needed to fetch from input elements of HTML page to PHP page. And further want to embed the code either in header or footer of website page in order to retrieve the values of input fields without knowing their names.

Example

<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="/action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form> 

<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>

</body>
</html>

Here, in above HTML code, although input box element names are mentioned. But suppose developer does not know the names and he/she has to fetch every input values without knowing its names.

Query

It is the requirement to work this code in any page after embedding into header or footer. Any solution would be appreciated.

  • 2
    Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [What topics to avoid](https://stackoverflow.com/help/dont-ask) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and also [take the tour](http://stackoverflow.com/tour) – Milan Chheda Aug 08 '17 at 14:55
  • How is this a different question? Text boxes will be in the `$_REQUEST`, `$_POST` or `$_GET` array. – Jay Blanchard Aug 08 '17 at 14:57
  • You can use javascript to loop through the form to get all input fields. But in php you can only use Post, Get, Request to get the form data. – Rajendran Nadar Aug 08 '17 at 15:02
  • I want only text box input in php without fetching the name of text boxes –  Aug 10 '17 at 13:44

2 Answers2

1

In PHP, you can use a trick, adding a prefix as name (txt_data1, txt_data2), of each text box.

Use a HTML code as this:

<input type="text" name="txt_data1">
<input type="text" name="txt_data2">

In PHP use a code as this:

foreach($_POST as $field=>$value){
    if ($strpos($field,"txt_")===0){
        //Use $value of text box
    }
}

More detail : $_POST param names

Jorge SB
  • 155
  • 6
0

It is not really possible.

Specifically, it's not possible using PHP alone. You could achieve it with some help from a client-side script, but with just PHP, all you can do is count the values in $_POST (or $GET, if your form uses that method.) But this will just count all the values in any type of input, select, or textarea on the form. There isn't really a reliable way to determine whether a value was sent by an <input type="text"> or a different type of control.

For example, if you submit a form like this:

<form action="" method="post">
    <input type="checkbox" name="ex1" value="example" checked="checked">
    <input type="hidden" name="ex2" value="example">
    <input type="radio" name="ex3" value="example" checked="checked">
    <input type="text" name="ex4" value="example">
    <select name="ex5">
        <option value="example" selected="selected">Example</option>
    </select>
    <textarea name="ex6">Example</textarea>
    <input type="submit" name="ex7" value="example">
</form>

$_POST will look like this:

array (size=7)
  'ex1' => string 'example' (length=7)
  'ex2' => string 'example' (length=7)
  'ex3' => string 'example' (length=7)
  'ex4' => string 'example' (length=7)
  'ex5' => string 'example' (length=7)
  'ex6' => string 'Example' (length=7)
  'ex7' => string 'example' (length=7)
Don't Panic
  • 41,125
  • 10
  • 61
  • 80