Asked  2 Years ago    Answers:  5   Viewed   109 times

I have a form that is a little complex and I am hoping to simplify the server-side (PHP) processing by natively POSTing an array of tuples.

The first part of the form represents a User:

  • First Name
  • Last Name
  • Email
  • Address
  • etc

The second part of the form represents a Tree:

  • Fruit
  • Height
  • etc

The problem is that I need to be able to POST multiple Trees for a single User in the same form. I would like to send the information as a single User with an array of Trees but this might be too complex to do with a form. The only thing that comes to mind is using javascript to create some JSON message with a User object and an array of Tree objects. But it would be nice to avoid javascript to support more users (some people have scripts turned off).

 Answers

5

check this one out.

<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">

<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">

<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">

<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">

it should end up like this in the $_POST[] array (PHP format for easy visualization)

$_POST[] = array(
    'firstname'=>'value',
    'lastname'=>'value',
    'email'=>'value',
    'address'=>'value',
    'tree' => array(
        'tree1'=>array(
            'fruit'=>'value',
            'height'=>'value'
        ),
        'tree2'=>array(
            'fruit'=>'value',
            'height'=>'value'
        ),
        'tree3'=>array(
            'fruit'=>'value',
            'height'=>'value'
        )
    )
)
Friday, December 16, 2022
3
<?php

 $Input = $_POST['element'];

 $FileToUpdate = "home.php";
 $fh = fopen($FileToUpdate , 'w') or die("can't open file");

 fwrite($fh, $Input);

 fclose($fh);     

 ?>

The code above will do what you wish, but will overwrite the page (to append see this reference). But really I think you need to start from basics with a good PHP Tutorial.

Saturday, September 3, 2022
 
3

@Emily With respect to your updated questions, its easy to achieve. Pass the array itself to ajax post like this

$.ajax({
     url: 'reward.php',
     data: {'requestids' : response.data},
    type: "POST",
    success: function(response){
      if(response==1){
         alert("Reward Applied!");
             }
      else{
         alert("Reward Failed!");
             }
        }
   });
  }
}

Then in your PHP loop through requestids array and generate mysql query to insert all records at once like this

$sql= "INSERT INTO requestids (requestid) VALUES "; 
foreach($_POST['requestids'] as $requestId){
       $sql.="($requestId)";
}

I am sure you can easily convert this simple query into a PDO statement

Thursday, September 22, 2022
 
sean_f
 
3

Your input doesn't have a name attribute. That is what the client will pass along to the server. Flask will raise a 400 error if you access a form key that wasn't submitted.

<input name="my_input" id="my_input" type="text" value="{{ email }}">
Wednesday, August 10, 2022
 
3

One way to solve this is to reinforce your intentions by expliciting formmethod="post", like this:

<button type="submit" formmethod="post" formaction="add_user.php">Submit</button>
Monday, August 1, 2022
 
redbite
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages