How to fill out a form using PHP and XPatch - php

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>

Related

Problems with passing data to twlio

Controller
public function searchNew()
{
$sid = //account_sid;
$token = //auth_token;
$client = new Services_Twilio($sid, $token);
$country = $this->input->post('select_country');
$type = $this->input->post('type');
$params = array(
'Contains' => $this->input->post('contains'),
'SmsEnabled' => $this->input->post('SMS'),
'MmsEnabled' => $this->input->post('MMS'),
'VoiceEnabled' => $this->input->post('Voice'),
'ExcludeAllAddressRequired' => TRUE,
'ExcludeLocalAddressRequired' => TRUE,
'ExcludeForeignAddressRequired' => TRUE
);
$numbers = $client->account->available_phone_numbers->getList($country, $type, $params);
return $numbers;
}
View
<form action="<?php echo base_url('tenant/phone_numer/searhNew');?>" method="post">
<input type="text" name="contains" class="form-control mbottom1" id="user_firstname" placeholder="Enter a description name for your number">
<select name="select_country" id="select_country" class="form-control">
<option disabled selected>Country</option>
<option value="US">USA (+1)</option>
</select>
<input id="checkcap1" type="checkbox" name="Voice" class="radio1" value="True"/>Voice
<input id="checkcap2" type="checkbox" name="SMS" class="radio1" value="True"/>SMS
<input id="checkcap3" type="checkbox" name="MMS" class="radio1" value="True"/>MMS
<input id="checktype1" type="radio" name="type" class="radio2" value="Local"/>Local
<input id="checktype2" type="radio" name="type" class="radio2" value="Mobile"/>Mobile
<input id="checktype3" type="radio" name="type" class="radio2" value="TollFree"/>Toll-Free
<input id="checktype3" type="radio" name="type" class="radio2" value="National"/>National
<button type="Submit">Test Submit</button>
</form>
I need help in getting the $country,$type,$params from my view file. I have done a few testing and when I change $country = $this->input->post('select_country'); to $country = "US" and the other variables into a static value, the code works fine, but when I change it their respective inputs the error message I am receiving is
The requested resource
/2010-04-01/Accounts/acount_sid/AvailablePhoneNumbers//Tollfree.json
was not found
Thanks for any help.
Do it with step by step flow of codeigniter.. After submit form , In controller print your post data..
print_r($_POST);
Check all data come from Form.. And check all by isset and pass in params,type , category. It will working fine..One More thing , In twilio library all parameters passing are important e.g.
$params=array();
if(isset($this->input->post('Voice'))){
$params['VoiceEnabled']=$this->input->post('Voice');
}

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...

Pregmatch form fields that are not hidden, retrieve only names

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";
}

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.

Organizing the post array

Here is my form
<form name="input" action="http://localhost/shopper/index.php?route=module/cart/insert_shopper" method="post">
<input quantity="4" type="hidden" name="28" value="1">
<input type="hidden" quantity="3" name="29" value="1">
<input type="submit" value="Submit" />
I have this format
[post] => Array
(
[28] => 1
[29] => 1
)
I really want the post array to be products and then a list on product ids with quantities ...is there an easy way to change the form
so on the next page i can do this
$products = $_POST['products']
foreach( $products as $p)
{
if( isset($p) && $p<>'')
{
///// place your code here
}
}
<input type="hidden" name="products[29][3]" value="1">
$products = $_POST['products']
foreach( $products as $product_id=>$quantity)
{
echo $product_id;
echo $quantity;
}
In your HTML form you can add array brackets after the end of the name attribute value, eg:
<input type = 'hidden' name = 'products[]' value = '12'/>
<input type = 'hidden' name = 'qty12' value = '16'/>
<input type = 'hidden' name = 'products[]' value = '13'/>
<input type = 'hidden' name = 'qty13' value = '72'/>
<input type = 'hidden' name = 'products[]' value = '14'/>
<input type = 'hidden' name = 'qty14' value = '1'/>
The PHP in the receiving script will then treat $_POST['products'] as an array. The quantity can be seen by:
foreach($_POST['products'] as $k => $v) {
echo($_POST['qty'.$v] . "<br/>");
}
<form name="input" action="" method="post">
<input type="hidden" name="29" value="3">
<input type="hidden" name="28" value="4">
<input type="hidden" name="27" value="2">
<input type="submit" value="Submit"/>
<?php
if (!empty($_POST))
{
foreach ($_POST as $id => $quantity)
{
echo 'ID: '.$id.' quantity: '.$quantity;
}
}
<input type="hidden" name="28" value="4">
<input type="hidden" name="29" value="3">
Will give you
[post] => Array
(
[28] => 4
[29] => 3
)

Categories