nsilva
nsilva

Reputation: 5614

PHP - Adding text values of multiple text box values to a variable

I want to add the following information to a variable, (The txt-via-hn textboxes are house numbers, the txt-via are address details)

$vias =         $_POST['txt-via-hn2'].' '.$_POST['txt-via2'] 
               .$_POST['txt-via-hn3'].' '.$_POST['txt-via3']
               .$_POST['txt-via-hn4'].' '.$_POST['txt-via4']
               .$_POST['txt-via-hn5'].' '.$_POST['txt-via5']
               .$_POST['txt-via-hn6'].' '.$_POST['txt-via6']
               .$_POST['txt-via-hn7'].' '.$_POST['txt-via7']
               .$_POST['txt-via-hn8'].' '.$_POST['txt-via8']
               .$_POST['txt-via-hn9'].' '.$_POST['txt-via9']
               .$_POST['txt-via-hn10'].' '.$_POST['txt-via10'];

I'm fairly new to PHP so I'm not too sure how to go about doing this but basically the format that I need this to be in is as follows:

$vias = <vias>
        <vh1>$_POST['txt-via-hn2']</vh1><vi1>$_POST['txt-via2']</vi1>
        <vh2>$_POST['txt-via-hn3']</vh2><vi2>$_POST['txt-via3']</vi2>
        <vh3>$_POST['txt-via-hn4']</vh3><vi3>$_POST['txt-via4']</vi3>
        <vh4>$_POST['txt-via-hn5']</vh4><vi4>$_POST['txt-via5']</vi4>
        <vh5>$_POST['txt-via-hn6']</vh5><vi5>$_POST['txt-via6']</vi5>
        <vh6>$_POST['txt-via-hn7']</vh6><vi6>$_POST['txt-via7']</vi6>
        <vh7>$_POST['txt-via-hn8']</vh7><vi7>$_POST['txt-via8']</vi7>
        <vh8>$_POST['txt-via-hn9']</vh8><vi8>$_POST['txt-via9']</vi8>
        <vh9>$_POST['txt-via-hn10']</vh9><vi9>$_POST['txt-via10']</vi9>
        </vias>

Also, if for example $_POST['txt-via-hn10'] & $_POST['txt-via10'] are empty, would it be possible that <vh9> and </vh9> are not included in the variable?

Any help on this would be much appreciated! :)

Upvotes: 1

Views: 1767

Answers (4)

Saeverix
Saeverix

Reputation: 397

You should start changing your form to get the values added to an array. You can do that by changing the name of the fields to 'house number[]'.

Do the same for the address detail fields.

Then loop trough the resulting two $_POST arrays and create the final xml like string.

I worked out a quick example below.

form.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title></title>
</head>
<body>
    <form action="action.php" method="post">
        <input type="text" name="housenumber[]" id="" />
        <input type="text" name="detail[]" id="" /><br />
        <input type="text" name="housenumber[]" id="" />
        <input type="text" name="detail[]" id="" /><br />
        <input type="text" name="housenumber[]" id="" />
        <input type="text" name="detail[]" id="" /><br />
        <input type="text" name="housenumber[]" id="" />
        <input type="text" name="detail[]" id="" /><br />
        <input type="text" name="housenumber[]" id="" />
        <input type="text" name="detail[]" id="" /><br />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

action.php:

<?php
$total_housenumbers = count($_POST['housenumber']);
$vias = '';

for ($i = 0; $i < $total_housenumbers; $i++) {
    if ($_POST['housenumber'][$i] != '' && $_POST['detail'][$i] != '') {
        $vias .= $_POST['housenumber'][$i]. ' ' .$_POST['detail'][$i]. '<br />';
    }
}

echo $vias;
?>

Upvotes: 0

MightyMouse
MightyMouse

Reputation: 351

The most straight forward way of doing this is by using an if statement per line to check if the $_POST variable exists and to concatenate them if true:

$vias = "<vias>";
$vias .= (isset($_POST['txt-via-hn2']) && isset($_POST['txt-via2'])) ? "<vh1>" . $_POST['txt-via-hn2'] . "</vh1><vi1>" . $_POST['txt-via2'] . "</vi1>    " : null;
.
.
.
$vias .= "</vias>";

Or you can use a FOR loop to iterate since you are using a well declared numeric sequence here:

$vias = "<vias>";
for ($x = 1; $x < 10; ++$x) {
    $vias .= (isset($_POST['txt-via-hn'.($x+1)]) && isset($_POST['txt-via'.($x+1)])) ? "<vh$x>" . $_POST['txt-via-hn'.($x+1)] . "</vh$x><vi$x>" . $_POST['txt-via'.($x+1)] . "</vi$x>    " : null;
}
$vias .= "</vias>";

Upvotes: 1

kawashita86
kawashita86

Reputation: 1563

to check if a variable exists you can use isset() function to check it and then decide what to print based on this. using conditional expression it become:

(isset($_POST['txt-via-hn10']) && isset($_POST['txt-via10']))
?'<vh9>'.$_POST['txt-via-hn10'].'</vh9><vi9>'.$_POST['txt-via10'].'</vi9>'
: ''

as for the string you're trying to generate since the variable are similar you can use a for loop, with the condition i've wrote above inside it to check the existance of the variables and add the row to the $vias string that you have initialized outside the loop:

$number_of_field = count($_POST);
$vias = '<vias>';
for($i = 1; $i<=$number_of_field; $i++){
 $vias .=(isset($_POST['txt-via-hn'.$i]) && isset($_POST['txt-via'.$i]))
?'<vh'.($i-1).'>'.$_POST['txt-via-hn'.$i].'</vh'.($i-1).'><vi'.($i-1).'>'.$_POST['txt-via'.$i].'</vi'.($i-1).'>'
: ''
}
$vias .= '</vias>';

Upvotes: 0

McLosys Creative
McLosys Creative

Reputation: 759

Use the php isset() to test weather a variable is set or not. and use code as follow:

$vias = "<vias>";

if(isset($_POST['txt-via-hn2'])
{
$vas=$vas."<vh1>".$_POST['txt-via-hn2']."</vh1>";       
}

if(isset($_POST['txt-via2'])
{
$vas=$vas."<vi1>".$_POST['txt-via2']."</vi1>";      
}

... and continue as you need..



and at last 
$vias=$vias." </vias>"; 

Upvotes: 0

Related Questions