Pregmatch form fields that are not hidden, retrieve only names - php

I'm trying to scan a form, and only pull out the fields that are not type="hidden" and retrieve their name="" value, I'm currently using
#<input.*?>#
Which retrieves the following for me,
(
[0] => Array
(
[0] => <input type="email" id="Contact0Email" name="email" class="field" onfocus="if ($(this).val() == $(this).attr('title')) { $(this).val('') }" onblur="if ($(this).val()=='') { $(this).val($(this).attr('title'));}" title="Enter a valid email here" value="Enter a valid email here">
[1] => <input type="submit" class="submit" value="Get Instant Access">
)
However I do not need all the code, I'd have to scan further to get what I need, anyone could suggest what regular expression I should use to get what I need? In this example there was no hidden fields, however there may be in some others I need to run this for.

Here is quick and dirty solution for you:
$string = '<form name="input" action="html_form_action.asp" method="get">
<input type="hidden" name="foo" value="123"/>
Username: <input type="text" name="user" value="Ralph">
Pass: <input type="text" name="pass">
<input type="submit" value="Submit">
</form>';
$doc = new DOMDocument();
$doc->loadHTML($string);
$input = $doc->getElementsByTagName('input');
for ($i = 0; $i < $input->length; $i++) {
$el = $input->item($i);
if ($el->getAttribute('type') === 'hidden'
|| $el->getAttribute('type') === 'submit') continue;
echo $el->getAttribute('name')
.':'.$el->getAttribute('value')."\n";
}

Related

How to fill out a form using PHP and XPatch

I have this HTML code and I would like it to be automatically filled with parameters taken from the database/table:
Array
(
[0] => Array
(
[id] => 35
[nr_wyceny] => W/8/01041122/2021
[nazwa_firmy] => Test
[adres_firmy] => testowy
[kraj_firmy] => polska
[telefon_firmy] => 79280xxxx
[osoba_firmy] => ja
[email_firmy] => pow#d.pl
[data_inwestycji] => 12/12/2020
[wycena_wstepna] =>
[wycena_zrzutem] => on
[jakosc_niska] => on
[jakosc_srednia] =>
[jakosc_wysoka] =>
[wygoda_mala] => on
[wygoda_srednia] =>
[wygoda_maksymalna] =>
[cena_niska] => on
[cena_srednia] =>
[cena_wysoka] =>
[wydajnosc_niska] =>
[wydajnosc_srednia] =>
[wydajnosc_wysoka] => on
[srodki_wlasne] => on
[finansowanie_zewnetrzne] =>
[finansowanie_niepodane] =>
[dzialalnosc_uslugowa] => on
... //more data
The point is that I download an array with the names and values of input fields from the database, if there is a checkbox then the value is on or null if there is input type text then it should have a value, but I don't know how to fill in the form using XPatch - possibly some other method to fill it this form, without rewriting the code. I have a lot of values to fill in and would like to do it automatically. i know how to do it the other way, but i would like to use xpatch and a DOM parser
<form action="/xxxxxx.php" method="post" id="xForm" >
<div class="tabela">
<div id="sekcja1" class="sekcja">
<div class="headerx"><p><b>Dane klienta</b></p></div>
<p>Nazwa firmy: <input class="xForm" type="text" name="nazwa_firmy" value=""/>
<p>Adres: <input class="xForm" type="text" name="adres_firmy" value=""/>
<p>Kraj: <input class="xForm" type="text" name="kraj_firmy" value=""/>
<p>Telefon kontaktowy: <input class="xForm" type="text" name="telefon_firmy" value=""/>
<p>Osoba do kontaktu: <input class="xForm" type="text" name="osoba_firmy" value="" />
<p>Email: <input class="xForm" type="text" name="email_firmy" value=""/>
<p><b>Planowany termin realizacji inwestycji: <input class="xForm" type="text" name="data_inwestycji" id="datepicker" value=""/></b>
</div>
<div id="sekcja2" class="sekcja">
<div class="headerx"><p><b>Dane na temat formy preferowanej wyceny:</b></p></div>
<p>Wstępna wycena <input class="xForm" type="checkbox" name="wycena_wstepna" checked="" />
<p>Wycena docelowa z rzutem urządzeń <input class="xForm" type="checkbox" name="wycena_zrzutem" checked="" />
<br />
<!---// More form data more input fields //--->
</form>
My PHP code is:
<?php
$url = file_get_contents('/wycena.html');
$url = mb_convert_encoding($url, 'utf-8', mb_detect_encoding($url));
$url = mb_convert_encoding($url, 'html-entities', 'utf-8');
$dom = new DOMDocument();
$dom->loadHTML($url);
$xpath = new DOMXpath($dom);
$html = $dom->saveHTML();
$dom->loadHTML($html);
// in this case i have fill only one field of input by name "nazwa_firmy" //and value "testtest" only for testing
//=======================================================
$key="nazwa_firmy"; //name of input element - test data
$val="testest"; //value of input element - test data
//=======================================================
$item = $xpath->query('//input[#name = "'.$key.'"]/#type');
if($item[0]->nodeValue=="text"){
$itemx = $xpath->query('//input[#name = "'.$key.'"]/#value');
}
if($item[0]->nodeValue=="checkbox"){
$itemx = $xpath->query('//input[#name = "'.$key.'"]/#checked');
}
$itemx[0]->nodeValue = $val;
echo $html = $dom->saveHTML();
?>
Why is it not possible to fill in the form in this way, is there any method to fill it in?
If you aleady have $array containing the values, just echo them inside the value attrubute of the form: -
<p>Nazwa firmy: <input class="xForm" type="text" name="nazwa_firmy" value="<?= $array [nazwa_firmy] ?>"/>
<p>Adres: <input class="xForm" type="text" name="adres_firmy" value="<?= $array [adres_firmy] ?>"/>
<p>Kraj: <input class="xForm" type="text" name="kraj_firmy" value="<?= $array [kraj_firmy] ?>"/>
<p>Telefon kontaktowy: <input class="xForm" type="text" name="telefon_firmy" value="<?= $array [telefon_firmy] ?>"/>
<p>Osoba do kontaktu: <input class="xForm" type="text" name="osoba_firmy" value="<?= $array [osoba_firmy] ?>" />
<p>Email: <input class="xForm" type="text" name="email_firmy" value="<?= $array [email_firmy] ?>"/>
<p><b>Planowany termin realizacji inwestycji: <input class="xForm" type="text" name="data_inwestycji" id="datepicker" value="<?= $array [data_inwestycji] ?>"/></b>
</div>

PHP simple form not posting

I've been tearing my hair out trying to figure out why the isset($_POST['Submit']) is not executing with my form. The data from the form is just not passing into the php code. Basically the code does not seem to be recognizing something like $ffname = $_POST["ffname"];
<?php
$ffname = $flname = $femail = $fcemail = $fpass = $fcpass = "";
if(isset($_POST['ffname'])){
$ffname = $_POST["ffname"];
$flname = $_POST["flname"];
$femail = $_POST["femail"];
$fcemail = $_POST["fcemail"];
$fpass = $_POST["fpass"];
$fcpass = $_POST["fcpass"];
echo "<p>Hello World<p>";
$con = mysqli_connect("localhost", "root", "") or die(mysqli_error());
mysqli_select_db($con, "userdata") or die(mysqli_error($con));
mysqli_query($con,"INSERT INTO tbluser (fname, lname, email, pass) VALUES('$ffname', '$flname', '$femail', '$fpass')")
or die (mysqli_error($con));
}
?>
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
The other answer by #DerVO is correct. But there seems to be something else at play, since you say it still doesn't work.
A comment became too long, so I've built a full answer here.
Step 1:
Add a name to your input:
<input type="submit" name="Submit" value="submit">
However, relying on the submit in your $_POST is not the best plan. So I suggest watching a different form field - for example, ffname:
Step 2:
Improve your watch, using a different field:
if ( isset( $_POST['ffname'] ) ) {
// do your work
}
Lastly, you may be munging your form action attribute.
Step 3:
In order to keep things simple, if the form is supposed to submit to the same page, you can simply omit the form action.
<form method="post">
Betweeen these three items, the form will work, unless you have some problem with your server.
Step 4:
Clean up your form formatting. You've got odd spacing which is problematic. In an html element, the property="value" code needs to be without spaces, but spaces between properties. Example:
<!-- Your version -->
<input type = "text"name = "ffname"id = "ffname"value="<?php echo $ffname;?>"><br>
<!-- Clean / correct version -->
<input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Here's a "clean" version of your whole form:
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
You need to give your input submit a name:
<input type="submit" name="Submit" value="Submit">
You have pass name of element in $_POST
try put name attribute in input submit
<input type = "submit" name="Submit" value = "1">

PHP For loop for $_POST

Sorry for the noob question. But I am stuck here.
This is my HTML form where the user-form div can be cloned to as many as possible. The #submit-form div has some hidden values which are common for all.
HTML -
<div class="user-form">
<input type="text" autocomplete="off" name="name[]" >
<input type="email" autocomplete="off" name="mail[]" >
</div>
<div class="user-form">
<input type="text" autocomplete="off" name="name[]" >
<input type="email" autocomplete="off" name="mail[]" >
</div>
<div id="submit-form">
<input type='hidden' name='refer_user_id' value='<?php echo $refer_user_id ?>'>
<input type='hidden' name='refer_user_email' value='<?php echo $refer_user_email ?>'>
<input type="submit" value="Invite" />
<input type="button" class="button" id="clonetrigger" value="Clone" />
</div>
I'm using ajax to submit the form. Basically I want to create accounts using the name and email fields. In PHP How do I use foreach to loop through the name and email fields so that I can create unique accounts?
My print_r($_POST); array looks like this.
Array
(
[name] => Array
(
[0] => david
[1] => Mark
[2] => cindy
)
[mail] => Array
(
[0] => david#abc.com
[1] => mark#abc.com
[2] => cindy#abc.com
)
[refer_user_id] => 2
[$refer_user_email] => test#abc.com
)
Create a loop with a number of iterations equal to the number of submitted name/email pairs, then use the loop counter to access the values for each user.
for ($i = 0; $i < count($_POST['name']); $i++) {
{
$name = $_POST['name'][$i];
$mail = $_POST['mail'][$i];
// Process the new user
}
go through one of the arrays with a foreach, use the key for the second array.
foreach($_POST['name'] as $key =>$name ){
$mail = $_POST[$key];
}
foreach($_POST['name'] as $key => $val) {
echo $val
}
foreach($_POST['mail'] as $key => $val) {
echo $val
}
Easiest way to loop through those elements. You can reference the other elements with $_POST['refer_user_id']. While this works for the purposes of a foreach, the for loop posted above is more efficient, so I'd recommend using it.
http://php.net/manual/en/control-structures.foreach.php More reading on it here.
You can use array_combine function:
$data = array_combine($_POST['name'],$_POST['mail']);
foreach($data as $name=>$mail){
print $name;
//...
}
See array_combine.
You could also use the JavaScript that's auto-generating the form items to give them a name that would result in linking the php object. i.e.
<div class="user-form">
<input type="text" autocomplete="off" name="user[1][name]" />
<input type="email" autocomplete="off" name="user[1][mail]" />
</div>
<div class="user-form">
<input type="text" autocomplete="off" name="user[2][name]" />
<input type="email" autocomplete="off" name="user[2][mail]" />
</div>
Then you could loop through the pairs with foreach($_POST['user'] as $key=>$value) etc...

insert batch data array?

I want insert following data by insert_batch as in following example in database table (mysql):
HTML:
<input name="u_id[0][0]" value="76">
<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">
<input name="u_id[1][0]" value="77">
<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">
<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">
PHP:
$u_id = $this->input->post('u_id');
$un = $this->input->post('un');
$up = $this->input->post('up');
$ue = $this->input->post('ue');
$data = array();
foreach ($un as $idx => $name) {
$data[] = array(
'u_id' => $u_id[$idx],
'un' => $un[$idx],
'up' => $up[$idx],
'ue' => $ue[$idx],
);
};
$this -> db -> insert_batch('units', $data);
I want insert they as this:
How should change php code and html code? what do i do?
I am assuming you are using CodeIgniter and that the name of the database table that you want to insert to is called 'units' and that its 'id' column is autoincrement.
I am basing off my solution from CodeIgniter User Guide Version 2.0.3 using a call to a helper ($this->db->insert_string()) of the Database class.
foreach ($data as $row)
{
$error_code = $this->db->insert_string('units', $row);
}
Refer to http://codeigniter.com/user_guide/database/helpers.html
The insert_string function that the Database class provides appears to take an associative array, build an INSERT statement from the elements inside, execute it and then return a numerical error code.
LOL, it's not pretty, but it might work sometimes:
<input name="u_id[]" value="76">
<input name="un[]" value="1">
<input type="text" name="ue[]" value="11">
<input type="text" name="up[]" value="111">
<input name="u_id[]" value="77">
<input name="un[]" value="2">
<input type="text" name="ue[]" value="22">
<input type="text" name="up[]" value="222">
<input name="un[]" value="3">
<input type="text" name="ue[]" value="33">
<input type="text" name="up[]" value="333">
$u_id=$this->input->post('u_id');
$un=$this->input->post('un');
$up=$this->input->post('up');
$ue=$this->input->post('ue');
for($i=0;$i<count($u_id);$i++){
for($ii=0;$ii<count($un[$i]);$ii++){
(count($un[$i])>1)?$unn=$un[$i][$ii+1]:$unn=$un[$i][$ii];
(count($ue[$i])>1)?$uen=$ue[$i][$ii+1]:$uen=$ue[$i][$ii];
(count($up[$i])>1)?$upn=$up[$i][$ii+1]:$upn=$up[$i][$ii];
$this->db->insert('units', array(//use db insert here
'u_id'=>$u_id[$i][0],
'un'=>$unn,
'ue'=>$uen,
'up'=>$upn,
));
}
}
I'd go so far as to suggest you not use it. But perhaps it might inspire someone to offer a better solution.
Cheers.

Upload multiple form $_post arrays to database

I'm trying to upload a form to the database, that uses the same field names, as part of a CMS.
This is the form (I have used JQuery to multiply the div the number of options per product):
<form method="post" enctype="multipart/form-data" action="testing.php">
<div class="OptExtra1">
<h3>Additional Option</h3>
<label for="RESAddType">File type (i.e. “CD” or “Download”)</label>
<input name="RESAddType[]" type="text" id="RESAddType" size="48" class="FW" />
<label for="RESAddTitle">File title (i.e. “Boxed Set of 4 CDs”)</label>
<input name="RESAddTitle[]" type="text" id="RESAddTitle" size="48" class="FW" />
<label for="RESAddFType">File format (As “MP3” // “WORD” // “PDF”)</label>
<input name="RESAddFType[]" type="text" id="RESAddFType" size="48" class="FW" />
<label for="RESAddPrice">File price (Enter as “6.99” – <strong>NO “£” SIGN!</strong>)</label>
<input name="RESAddPrice[]" type="text" id="RESAddPrice" size="48" class="FW" />
<label for="RESAddFName">File name</label>
<input name="RESAddFName[]" type="text" id="RESAddFName" size="48" class="FW" />
<label for="RESAddTxt">File text</label>
<textarea name="RESAddTxt[]" id="RESAddTxt" cols="70" rows="50" class="mceAdvanced"></textarea>
<label for="RESAddSample">File text</label>
<textarea name="RESAddSample[]" id="RESAddSample" cols="70" rows="50" class="mceVSimple"></textarea>
<input type="button" value="Add another option" class="SubmitButton" onclick="inserter()" />
<hr />
</div>
<p><input type="submit" name="submit" value="Add Resource" class="SubmitButton"/><input type="hidden" name="RESCatCode" value="2" /><input type="hidden" name="RESCatSubCode" value="5" /><input type="hidden" name="submitted" value="true" /></p>
and this is what I have so far for the PHP
if(isset($_POST['submitted'])){
$RESCode = 100;
$RESAddType = $_POST['RESAddType'];
$RESAddTitle = htmlentities($_POST['RESAddTitle'], ENT_QUOTES);
$RESAddFType = $_POST['RESAddFType'];
$RESAddPrice = $_POST['RESAddPrice'];
$RESAddFName = $_POST['RESAddFName'];
$RESAddTxt = mysql_real_escape_string($_POST['RESAddTxt']);
$RESAddSample = mysql_real_escape_string($_POST['RESAddSample']);
for ($i=0; $i < count($_POST['RESAddType']); $i++) {
$OptionQuery = mysql_query ("INSERT INTO ResAdd (RESAddCode, RESCode, RESAddType, RESAddTitle, RESAddFType, RESAddPrice, RESAddFName, RESAddTxt, RESAddSample) VALUES ('', '".$RESCode."', '".$RESAddType."', '".$RESAddTitle."', '".$RESAddFType."', '".$RESAddPrice."', '".$RESAddFName."', '".$RESAddTxt."', '".$RESAddSample."');");
}
header("Location: welcome.php");
exit;
}
It kind of worked, but is just putting in the word "array" into the database. Also the htmlentities and mysql_real_escape_string posts don't upload anything to the database.
Any ideas please?
You've forgotten something :)
$OptionQuery = mysql_query ("INSERT INTO ResAdd (RESAddCode, RESCode, RESAddType, RESAddTitle, RESAddFType, RESAddPrice, RESAddFName, RESAddTxt, RESAddSample) VALUES ('', '".$RESCode[$i]."', '".$RESAddType[$i]."', '".$RESAddTitle[$i]."', '".$RESAddFType[$i]."', '".$RESAddPrice[$i]."', '".$RESAddFName[$i]."', '".$RESAddTxt[$i]."', '".$RESAddSample[$i]."');");
Look at all these $i in the code above. If you don't use them, the script will try to use the entire array (and trying to print or save an array as a string always results in printing "Array").
P.S.I've edited my answer. Sorry for the previous one, I've misunderstood the code and posted a wrong answer.
You need to looping
$cnt=count($_POST['RESAddType']);
for ($counter=0; $counter < $cnt; $counter++)
{
$_POST['RESAddType'][$counter]// to access the value
$_POST['RESAddPrice'][$counter]//
//create your query here
}

Categories