<form action="form.php" id="form">
<input type="text" name="name_1"><input type="text" name="city_1"><input type="text" name="country_1">
<input type="text" name="name_2"><input type="text" name="city_2"><input type="text" name="country_2">
<input type="text" name="name_3"><input type="text" name="city_3"><input type="text" name="country_3">
<input type="submit">
</form>
How is the best method for get this data in form.php file? This is generated with jQuery. Can be 3 (as now) or 30.
In form.php i have:
$data = new Data();
$data->name = $_POST['name_1'];
$data->city = $_POST['city_1'];
$data->country = $_POST['country_1'];
$data->save();
$data = new Data();
$data->name = $_POST['name_2'];
$data->city = $_POST['city_2'];
$data->country = $_POST['country_2'];
$data->save();
$data = new Data();
$data->name = $_POST['name_3'];
$data->city = $_POST['city_3'];
$data->country = $_POST['country_3'];
$data->save();
But if there is over 3? I would like use foreach, but how? How can i generated input or get this data?
Use name[], city[] and country[] as your input names. Then they'll come through to PHP as arrays which you can iterate with foreach.
If you can change the names of the input elements you could do it like that:
<input type="text" name="items[1][name]"><input type="text" name="items[1][city]"><input type="text" name="items[1][country]">
<input type="text" name="items[2][name]"><input type="text" name="items[2][city]"><input type="text" name="items[2][country]">
<input type="text" name="items[3][name]"><input type="text" name="items[3][city]"><input type="text" name="items[3][country]">
The numbers don't have to be adjacent but the have to be unique. To convert the $_POST-data to your data-model would then look like that:
$items = $_POST['items'];
$dataList = array();
foreach($items as $item) {
$data = new Data();
$data->name = $item['name'];
$data->city = $item['city'];
$data->country = $item['country'];
$dataList[] = $data;
}
Of course you can add as many items as you want.
If you can't change the names of the input elements then you could use one of the other solutions. I would only modify them so that the number of elements is not hard coded like:
$dataList = array()
for($i = 1; isset($_POST["name_$i"]); ++$i) {
$data = new Data();
$data->name = $_POST["name_$i"];
// ... and so on
$dataList[] = $data;
}
Of course, with that solution the numbers must be continuous/adjacent.
You can use your for each with on time built strings like 'name_.$i', with $i as a counter
You can use a for loop:
for ($i = 1; $i <= 3; $i++) {
$data = new Data();
$data->name = $_POST['name_'.$i];
$data->city = $_POST['city_'.$i];
$data->country = $_POST['country_'.$i];
$data->save();
}
But if you could change your html like:
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
<input type="text" name="name[]"><input type="text" name="city[]"><input type="text" name="country[]">
You will not need to know the count.(name_1,name_2 would be better to be the id but not name)
foreach ($_POST['name'] as $index => $name) {
$data = new Data();
$data->name = $name;
$data->city = $_POST['city'][$index];
$data->country = $_POST['country'][$index];
$data->save();
}
Related
This is My PHP code till now I tried
<?php
include_once("../include/connClass.php");
$db = new Database();
$conn = $db->getConnection();
if(isset($_POST["save"]))
{
$date = $_POST["txtDate1"];
}
$date = $_POST["txtDate1"];
$service = $_POST["txtService1"];
$charge = $_POST["txtCharge1"];
$amount = $_POST["txtAmount1"];
$unit = $_POST["txtUnit1"];
$total = $_POST["txtTotal1"];
for ($i = 0; $i <= count($date); $i++) {
try {
$sql = $conn->prepare("INSERT INTO backup_master (backup_date,
backup_service, backup_charge, backup_amount, backup_unit, backup_total)
VALUES (:backup_date, :backup_service, :backup_charge,
:backup_amount, :backup_unit, :backup_total)");
$sql->bindParam(':backup_date', $date);
$sql->bindParam(':backup_service', $service);
$sql->bindParam(':backup_charge', $charge);
$sql->bindParam(':backup_amount', $amount);
$sql->bindParam(':backup_unit', $unit);
$sql->bindParam(':backup_total', $total);
$query = $sql->execute();
// echo $query;
}
catch (PDOException $e) {
echo $sql . "<br />Error" . $e->getMessage();
}
}
?>
this is my form code
<form method="POST" action="backend/save.php">
<input type="text" id="txtDate1" name="txtDate1">
<input type="text" id="txtService1" name="txtService1">
<input type="text" id="txtCharge1" name="txtCharge1">
<input type="text" id="txtAmount1" name="txtAmount1">
<input type="text" id="txtUnit1" name="txtUnit1">
<input type="text" id="txtTotal1" name="txtTotal1">
<button type="submit" name="save" class="btn btn-primary">Save</button>
</form>
Here is my question is I have an input boxes values with comma separated and I want to insert values one by one to the different rows to the database but now
all comma separated values are inserting into the single row any idea how to do Thanks in advance.
to broad question. you can explode string using explode function
HTML
<input type="text" name="dataString">
PHP
$dataString=$_GET['dataString'];
$testArray = explode(',', $dataString);
print_r($testArray);
I have a problem to replace an XML node while entering data into a form
My code below :
$xml = new DOMDocument("1.0", "ISO-8859-1");
$xml->preserveWhiteSpace = FALSE;
$xml->formatOutput = TRUE;
$xml->load($fichier);
$xpath = new DOMXPATH($xml);
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/flagplus");
foreach($query as $result){ $flagplus = $result->textContent;}
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/entete");
foreach($query as $result){ $entete = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/nouveau");
foreach($query as $result){ $nouveau = $result->textContent;}
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/description");
foreach($query as $result){ $description = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/couleur");
foreach($query as $result){ $couleur = $result->textContent;}
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/detail");
foreach($query as $result){ $detail = utf8_encode($result->textContent);}
$detail = str_replace('<br/>', PHP_EOL, $detail);
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/autre");
foreach($query as $result){ $autre = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/contact");
foreach($query as $result){ $contact = utf8_encode($result->textContent);}
$query = $xpath->query("/$racine/annonce[#id = '$annonce_no']/vente");
foreach($query as $result){ $vente = utf8_encode($result->textContent);}
$node = $xpath->query("/$racine/annonce[#id = '$annonce_no']");
if (isset($_POST['submitSave'])){
$id_new = $_POST['id'];
$flagplus_new = $_POST['flagplus'];
$entete_new = htmlentities($_POST['entete'], ENT_XML1);
$nouveau_new = $_POST['nouveau'];
$description_new = htmlentities($_POST['description'], ENT_XML1);
$couleur_new = $_POST['couleur'];
$detail_new = htmlentities($_POST['detail'], ENT_XML1);
$autre_new = htmlentities($_POST['autre'], ENT_XML1);
$contact_new = htmlentities($_POST['contact'], ENT_XML1);
$vente_new = htmlentities($_POST['vente'], ENT_XML1);
$node->replaceChild($id_new, $id);
$node->replaceChild($flagplus_new, $flagplus);
$node->replaceChild($entete_new, $entete);
$node->replaceChild($description_new, $description);
$node->replaceChild($couleur_new, $couleur);
$node->replaceChild($detail_new, $detail);
$node->replaceChild($autre_new, $autre);
$node->replaceChild($contact_new, $contact);
$node->replaceChild($vente_new, $vente);
$xml->save($fichier);
I want to change the new values with the old values.
The DOM documentations say : public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
and the form
<form method="post" class="thumbnail">
<div>
<p>ID</br>
<input type="text" name="id" style="width:30px;" maxlength="3" value="<?php echo $annonce_no ?>" ></p>
</div>
<div>
<p>2ème drapeau</br>
<input type="text" name="flagplus" style="width:20px;" maxlength="2" value="<?php echo $flagplus ?>" > blanc,FR,GE,VD,VS</p>
</div>
<div>
<p>Nouveau</br>
<input type="text" name="nouveau" style="width:10px;" maxlength="1" value="<?php echo $nouveau ?>" > 0=Non, 1=Oui</p>
</div>
<div>
<p>Couleur description</br>
<input type="text" name="couleur" style="width:10px;" maxlength="1" value="<?php echo $couleur ?>" > blanc ou R = rouge</p>
</div>
<div>
<p>Entete</br>
<input type="text" name="entete" style="width:20%" value="<?php echo $entete ?>" ></p>
</div>
<div>
<p>Description</br>
<input type="text" name="description" style="width:80%" value="<?php echo $description ?>" ></p>
</div>
<div>
<p>Détail annonce</br>
<textarea name="detail" style="width:80%" cols="200" rows="8"><?=$detail;?></textarea></p>
</div>
<div>
<p>Autre</br>
<input type="text" name="autre" style="width:80%" value="<?php echo $autre ?>" ></p>
</div>
<div>
<p>Contact</br>
<input type="text" name="contact" style="width:80%" value="<?php echo $contact ?>" ></p>
</div>
<div>
<p>Vente</br>
<input type="text" name="vente" style="width:20%;" value="<?php echo $vente ?>" ></p>
</div>
<div align="center"><input type="submit" name="submitSave" value="Enregistrer" class="btn btn-primary" style="width:150px"></div>
</form>
I have the message : Fatal Error: Call to undefined function replaceChild
I've looked to How to update xml file using PHP? it seems that works for him.I did the same think...
Any idea? thanks!
$node = $xpath->query("/$racine/annonce[#id = '$annonce_no']"); does not give you a DOM node, it is a DOM node list. So you at least need $node = $xpath->query("/$racine/annonce[#id = '$annonce_no']")->item(0); to have an object that exposes a replaceChild method. However, the other variables assigned with e.g. $flagplus_new = $_POST['flagplus']; are not nodes at all you could pass to that method, nor are the variables $flagplus = $result->textContent; which are all string values. If you want to replace an element's content with a new value then selecting that element and setting the textContent seems like the right approach.
So assuming e.g.
$node = $xpath->query("/$racine/annonce[#id = '$annonce_no']")->item(0);
gives you the right element then you can select the description child with e.g.
$descEl = $xpath->query('description', $node)->item(0);
and changes its contents with e.g.
$descEl->textContent = $newDescription;
The only drawback is that the documentation http://php.net/manual/en/class.domnode.php says "5.6.1 The textContent property has been made writable (formerly it has been readonly). ".
I want to send the "fill[]" and the "item[]" and my problem is the function write cant get the values of the variables, why?
What is wrong? Im new in this stuff.
<form action="/fill_sites/fill.php" method="get">
<input type="text" size="24" maxlength="20" name="fill[0]"/>
<input type="hidden" name="item[0]value="hella_apfelschorle"/>
<input type="text" size="24" maxlength="20" name="fill[1]"/>
<input type="hidden" name="item[1]" value="volvic_wasser"/>
..... so on
<input type="submit" value="Abschicken"/>
------------------------------------------------------------>[fill/php]>
$fill[0] = $_GET["fill[0]"];
$fill[1] = $_GET["fill[1]"];
$fill[2] = $_GET["fill[2]"];
$fill[3] = $_GET["fill[3]"];
$fill[4] = $_GET["fill[4]"];
$fill[5] = $_GET["fill[5]"];
$fill[6] = $_GET["fill[6]"];
$fill[7] = $_GET["fill[7]"];
$fill[8] = $_GET["fill[8]"];
$fill[9] = $_GET["fill[9]"];
$item[0] = $_GET["item[0]"];
$item[1] = $_GET["item[1]"];
$item[2] = $_GET["item[2]"];
$item[3] = $_GET["item[3]"];
$item[4] = $_GET["item[4]"];
$item[5] = $_GET["item[5]"];
$item[6] = $_GET["item[6]"];
$item[7] = $_GET["item[7]"];
$item[8] = $_GET["item[8]"];
$item[9] = $_GET["item[9]"];
write($counterFilename[$item[]], $fill);
you can do this by following code. it will get complete array.
$item = $_Get['item'];
$fill= $_Get['fill'];
use php's foreach statement to retrieve the content of fill and item
$fills = $_GET['fill'];
foreach($fills as $fill)
{
//do something with $fill
}
and the same for $item
Im having trouble with this php mysql issue. I have 5 input fields with the same name, and foreach input field entered i was that to be saved in the database. But at the moment, if i input 2 fields, its inserting them plus 3 blank ones. How do i just insert the ones which are not empty
here is the html
<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />
<input type="text" name="tags[]" placeholder="Add Tags" />
here is the php
$tags = $_POST['tags'];
for($i = 0; $i < count($tags); $i++){
$tag = $_POST['tags'][$i];
$addtags = $gifs->addtags($tag, $id);
}
well, first thing is to sanitize your input, second thing is to trim each value and add this:
$tag = trim($tag);
if(!empty($tag)){
...code
Try This
$tags = $_POST['tags'];
for($i = 0; $i < count($tags); $i++){
$tag = trim($_POST['tags'][$i]);
if (!empty($tag))
$addtags = $gifs->addtags($tag, $id);
}
Remove the empty with array_filter():
$tags = array_filter($_POST['tags']);
foreach($tags as $tag) {
$addtags = $gifs->addtags($tag, $id);
}
Try this, You can use foreach
$tags = $_POST['tags'];
foreach($tags as $tag) {
$tag = trim($tag);
if (! empty($tag)) {
$addtags = $gifs->addtags($tag, $id);
}
}
I'm working on a page with many insert fields.
how can i shorten the following code?
$title_1 = $_POST['title_1'];
$content_1 = $_POST['content_1'];
$link_1 = $_POST['link_1'];
$img_link_1 = $_POST['img_link_1'];
$title_2 = $_POST['title_2'];
$content_2 = $_POST['content_2'];
$link_2 = $_POST['link_2'];
$img_link_2 = $_POST['img_link_2'];
$title_3 = $_POST['title_3'];
$content_3 = $_POST['content_3'];
$link_3 = $_POST['link_3'];
$img_link_3 = $_POST['img_link_3'];
You could loop through the $_POST array like this:
foreach ($_POST as $key => $value) {
${$key} = $value;
}
This will make your post variable like $_POST['title_1'] into $title_1
Remember your post names will have to be the exact names you want your variables to be referenced by.
I would do:
$howmany = 3; // How many sets of fields are submitted.
for($i=0;$i<$howmany;$i++){
$field[$i]['title'] = $_POST['title_'.$i];
$field[$i]['content'] = $_POST['content_'.$i];
$field[$i]['link'] = $_POST['link_'.$i];
$field[$i]['img_link'] = $_POST['img_link_'.$i];
}
Then you can access data in $field[1]['title'] form.
You can use extract (http://php.net/manual/en/function.extract.php): extract($_POST)
But you should be careful -- what if the client POSTs user_id, or something? At the least, you should specify that $_POST values won't overwrite already-defined variables: extract($_POST, EXTR_SKIP)
I redid this answer after you edited your post. Use variable variables.
foreach ($_POST as $key => $val)
{
if (preg_match('/^(([a-z]+_)+\d)$/', $key, $match)
{
$$match[0] = $val;
}
}
Use [a-z0-9] or [a-zA-Z0-9] as alternatives.
<?php
$key_prefixes = array (
'title',
'content',
'link',
'img_link'
);
$i = 1;
while (true) {
$post_values_missing = 0;
foreach ($key_prefixes as $key_prefix) {
$key = $key_prefix . '_' . $i;
if (!isset($_POST[$key])) {
$post_values_missing += 1;
continue;
};
$val = $_POST[$key];
// do something with $val
}
// did you get any values through this iteration?
$post_values_exist_bool = (count($key_prefixes) !== $post_values_missing);
// if not, you must've gotten them all
if (false === $post_values_exist_bool) {
break;
}
$i += 1;
}
The cleanest way to do this would be to use PHP's POST data processing capabilities to do the work for you.
Consider using your HTML form names as follows:
<form action="{url}" method="post">
<input type="text" name="data[0][title]" />
<input type="text" name="data[0][content]" />
<input type="text" name="data[0][link]" />
<input type="text" name="data[0][image_link]" />
<input type="text" name="data[1][title]" />
<input type="text" name="data[1][content]" />
<input type="text" name="data[1][link]" />
<input type="text" name="data[1][image_link]" />
...
</form>
In PHP extract the data as follows:
$data = $_POST['data'];
This shortens your PHP code to just one line. This statement will directly give you an array in PHP of the data form input. A var_dump will look as follows:
array (
0 => array('title'=>'...','content'=>'...','link'=>'...','image_link'=>'...'),
1 => array('title'=>'...','content'=>'...','link'=>'...','image_link'=>'...'),
...
)
You don't have to change your name, just make it array;
<input type="text" name="title[]" />
<input type="text" name="content[]" />
<input type="text" name="link[]" />
<input type="text" name="image_link[]" />
<input type="text" name="title[]" />
<input type="text" name="content[]" />
<input type="text" name="link[]" />
<input type="text" name="image_link[]" />
<input type="text" name="title[]" />
<input type="text" name="content[]" />
<input type="text" name="link[]" />
<input type="text" name="image_link[]" />
PHP:
extract($_POST);
$count=count($title);
for($i=0;$i<$count;$i++) {
//You can perform your any function on this loop, to get title use $title[$i]
}