Asked  7 Months ago    Answers:  5   Viewed   38 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

62

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'
        )
    )
)
Wednesday, March 31, 2021
 
Dunc
answered 7 Months ago
77
<?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.

Wednesday, March 31, 2021
 
JohnnyW
answered 7 Months ago
92

@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

Wednesday, March 31, 2021
 
DiglettPotato
answered 7 Months ago
63

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 }}">
Tuesday, June 1, 2021
 
keyBeatz
answered 5 Months ago
42

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>
Friday, July 30, 2021
 
Revent
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :