How to fetch the result data from HTML form? - php

sorry to ask this dummy question. I have an HTML form like below that will result in a bunch of data lines to be parsed later by my PHP codes. But I get a bit stuck for how to get the data before I can parse and process it.
<form method="post" action="https://domain.com/cgi-bin/cgi.exe">
<input name="exec" value="viewproduct" type="hidden">
<input name="customer" value="customer_name" type="hidden">
<input name="sku" value="sku_number" type="hidden"> <br>
<input type="submit">
</form>
I need to store the result data in a string variable that I can parse it later line by line into multiple arrays.

Submit the data to (via the action attribute) a PHP program instead of a compiled Windows executable
Use $_POST['form_control_name']

$_POST holds your formdata. Access data like:
$_POST['exec'];
To string with for example delimiter ','
implode(',', $_POST);

Use $_POST[] array.
$exec = $_POST['exec'];
$customer = $_POST['customer'];
$sku = $_POST['sku'];

Related

How to make array from several input type text using ID of input text?

I'm new in HTML and PHP world. I try to make a multi dimensional array with HTML input type text like this (i use CodeIgniter Framework) :
<form method="POST">
<input type="text" id="myTF[0][0]" values="A"/>
<input type="text" id="myTF[0][1]" values="B"/>
<input type="text" id="myTF[0][2]" values="C"/>
<input type="text" id="myTF[1][0]" values="D"/>
</form>
And i hope when i submit that form, i can use PHP code like this :
foreach($myTF as $index_first => $val){
foreach($val as $index_second => $val2){
echo($val2);
}
}
Is the code above is correct? Or other way exist to do that more better?
Taking the code I would print out the $_POST data and look at the array structure. Doing it off the top of my head I plugged the data into https://3v4l.org and got this:
https://3v4l.org/hD8Bv
I may have the structure incorrect but it is a handy tool to figure out if a piece of logic will work or not.

Add data to $_POST and submit to another page

I'm trying to add a value to $_POST data while it gets submitted to the target page as follows:
post.php
<?php $_POST['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
catch.php
<?php
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
but I cannot catch 'field1' on the other end. I don't want to use a hidden input field. How can I achieve that?
Thanks!
When you send the form, the $_POST data is reset and assumes only the inputs inside the form and a possible query string you may have appended to form action.
The best way to accomplish what you want is using hidden field but since you dont want it, you can append a query string to your form action:
<form method="post" action="catch.php?field1=Value1">
You're not submitting field1 anywhere. What happens is this:
post.php generates a HTML page (one that doesn't contain any reference to field1)
the user's browser renders the page
on submit, only the elements inside the form are submitted
catch.php receives the elements submitted above.
In other words, you need to get that value into your form:
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input name="field1" type="hidden" value="<?php echo htmlspecialchars($_POST['field1']) ?>"/>
<input type="submit" value="Submit" />
</form>
There is no other way to get the value into your POST data, if it's not present in the form. What you could do as a workaround is store the data in GET (size limit), session (concurrency issues - what happens when the user has two tabs open, each with different session data?), or cookies (size limit AND concurrency issues).
You can't do it this way. If you want to send the data you're trying to add to the POST there only through the users form, you are forced to also store it somewhere client side (e.g. a hidden field or a cookie). What you're doing right now is setting some value into the POST variable, but it gets overridden by the users form post (or rather the $_POST variable you're using after the form post is another instance).
What you could do instead to keep it serverside is save the value in the variable to the session of the user, then in the form post action server side get the value again (given the fact that you're using sessions). Lastly you could just store it in some table in a database, though I wouldn't do this.
Since $_POST are data sent by post method to script, you can not use it for another request directly. You need to compose and send another post request. The easiest way for you will be to use hidden input field/s.
Or you can choose another approach to make http post request, for example curl methods.
If you don't need data to be given by post method, you can save it in session, for example.
try this:
in post.php
<?php $_SESSION['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
in catch.php
<?php
if(isset($_SESSION['field1']))
{
$_POST['field1'] = $_SESSION['field1'];
unset($_SESSION['field1']);
}
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
Make sure you have started the session.
Note: you must use hidden elements or query string as other user suggested.

get custom attribute value in php

I am try to get the value of the input field with a custom attribute I have created using PHP. This is my code:
<form action="uploadform.php" method="post">
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>
//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>
The web browser doesn't know what to do with your custom attribute, so will simply ignore it. The only data sent when you submit the form is the values of "successful" elements. So your custom data will never be sent, and can never be read by the receiving script.
The best place to put such data is into hidden input fields. One possibility is to use names with square brackets in, which PHP automatically converts into arrays. e.g.
<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">
Populates an array like this:
$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';

Reading multiple textfield values using array in php

Can you please help me in this context.
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
</form>
I am using this code in a loop and I need to get all the values in another page.
How can I get these value as array in another page.
you should be able to use $_POST array in the sample.php page
PHP's $_POST Array, taken from the docs:
An associative array of variables passed to the current script via the HTTP POST method.
first, add s submit button to your form, it should look like this:
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
<input type="submit">
</form>
and in sample.php:
<?php
print_r($_POST);
?>
This is the most simple example,
once you get the hang of it, I recommend you read the docs on topics such as:
htmlspecialchars
htmlspecialchars — Convert special characters to HTML entities
You can access it as array
$_POST['first'][0] // 0 will be index for single
to list all just loop
foreach($_POST['first'] as $first) {
...
}
Your values will end up in $_POST, and because you're using the [] syntax, you'll get an array. So, to get your first array, do:
$first = $_POST['first'];
You could then iterate over it like so:
foreach ($first as $item) {
...
}
I think this will help you.
$first=$_POST['first'];
$second=$_POST['second'];
foreach($first as $key=>$first){
echo 'Your first value'.$first.'Your second value'.$second[$key];
}

Getting information from a form (textbox, combobox) and writing to an xml file (PHP/JAVASCRIPT)

Hi how would i GET information from a form that a user enters into a textbox combobox or w.e
and write it to a xml file.
either HTML javascript or php will be appreciated.
Well, saying that you have a page with a form like:
<form name="form" action="getdata.php" method="post">
<input type="text" name="tb" />
<input type="submit" value="Submit" />
</form>
your getdata.php (this is just an example of the name) page, which will collect the data and save them in a xml file, should be like this:
<?php
$text = htmlentities($_POST['tb']);
$doc = new DOMDocument();
$textareaNode = $doc->createElement("textbox");
$textNode = $doc->createTextNode($text);
$textareaNode->appendChild($textNode);
$doc->appendChild($textareaNode);
$doc->save("data.xml");
?>
Here are some links that can get you started.
PHP SimpleXML
PHP $_POST
HTML form input types

Categories