0

I have a simple form.. where users will input data, and I want that data to be sent to the server upon clicking on save.

This is my form:

<form method="POST" name="admin_form" action=" ">
                    <div class="form-row">
                        <div class="form-group col-md-12">
                            <label for="inputEmail4">Product Name</label>


                            <input type="name" class="form-control" name="name" id="inputproductname"
                                placeholder="Product name">
                        </div>
                        <div class="form-group col-md-12">
                            <label for="inputPassword4">Description</label>
                            <textarea class="form-control" name="description" id="exampleFormControlTextarea1"
                                rows="3"></textarea>
                        </div>
                    </div>

                    <!-- start select shop -->
                    <div class="form-group">
                        <label for="exampleFormControlSelect2">Select shop</label>
                        <?php 


                $q = "SELECT * FROM heroku_50fc799819d.shops ORDER BY  name ASC";        
                $r = mysqli_query($conn,$q);
                    ?>
                        <select class="form-control" name="shops" id="shops" required>
                            <!-- <option value="0">Select category.</option> -->
                            <?php while ($data = mysqli_fetch_array($r)){
                $id = $data['id'];
                $name = $data['name']; 
                ?>
                            <option value="<?php echo $id;?>"> <?php echo $name; ?> </option>
                            <?php } ?>
                        </select>
                    </div>

                    <!-- end shop select -->

                    <!-- image select -->
                    <form>
                        <div class="form-group">
                            <label for="exampleFormControlFile1">Upload image product</label>
                            <input type="file" class="form-control-file" id="exampleFormControlFile1">
                        </div>
                    </form>

                    <!-- end image select -->

                    <!-- start category select -->
                    <div class="form-group">
                        <label for="exampleFormControlSelect1">Select one or multiple categories</label>

                        <?php 
                $q = "SELECT * FROM heroku_50fc799819d.categories ORDER BY  name ASC";        
                $r = mysqli_query($conn,$q);
                    ?>
                        <select multiple class="form-control" name="category" id="product_category" required>
                            <!-- <option value="0">Select category.</option> -->
                            <?php while ($data = mysqli_fetch_array($r)){
                $id = $data['id'];
                $name = $data['name']; 
                ?>
                            <option value="<?php echo $id;?>"> <?php echo $name; ?> </option>
                            <?php } ?>
                        </select>
                    </div>


                    <!-- end category select -->


                    <div class="form-group">
                        <label for="inputPrice">Price</label>
                        <input type="text" class="form-control" name="price" id="inputPrice" placeholder="12000">
                    </div>
                    <div class="form-group">
                        <label for="inputPrice">Discount</label>
                        <input type="text" class="form-control" name="discount" id="inputPrice" placeholder="10">
                    </div>

                    <div class="form-group">
                        <label for="inputPrice">Qty</label>
                        <input type="text" class="form-control" name="qty" id="inputPrice" placeholder="10">
                    </div>
                    <button type="submit" name="submit" class="btn btn-primary">
                        Save
                    </button>
                </form>

and this is how am trying to send the data:

$post_data = new \stdClass();
$final_post_data = new \stdClass();
$post_data ->name = $_POST['name']?? '';
$post_data ->description = $_POST['description']?? '';
$post_data ->shopId = $_POST['shops']?? '';
$post_data ->categories = [2, 16, 278];
$post_data ->price = $_POST['price']?? '';
$post_data ->quantity = $_POST['qty']?? '';

$post_dataJSON = json_encode($post_data);
$final_post_data ->body = $post_dataJSON;
$final_post_dataJSON = json_encode($final_post_data);
//initialize
$ch = curl_init('https://admin2go.herokuapp.com/api/admin/products/create');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//adding the post variable to the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $final_post_dataJSON);
//Return output instead of outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($final_post_dataJSON)
    )                                                                       
);  

$result = curl_exec($ch);

if ($result === FALSE) {
    echo "cURL Error: " . curl_error($ch);
}
//close and free up the curl handle
curl_close($ch);
//Display row output
print_r($result);

?>

i am currently getting this error message back from the server. so how do i pass the data from the form over by clicking on save?? {"error":"No product name passed"}

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DanielSmurts
  • 593
  • 5
  • 13
  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Nov 16 '19 at 21:55
  • yes I found an answer as to how to deal with the error ...so half my problem is solved.... the issue is how to get values from the form to the variable when the user clicks the save button – DanielSmurts Nov 17 '19 at 04:47
  • 1
    dude you're trying to upload images using json - images is **binary data**, and **json is not binary safe**, don't use json if you're handling binary data, use `multipart/form-data` or `application/x-www-form-urlencoded`, they are both perfectly capable of handling binary data (like images) (multipart/form-data has lower bandwidth overhead than x-www-form-urlencoded tho, the latter has in a worst case scenario about 66% overhead per byte of binary data, and multipart/form-data has about 0% overhead per byte, it just has some header overhead, but no per-byte overhead) – hanshenrik Nov 17 '19 at 15:54
  • @hanshenrik very good observation... actually I plan to send the images to firebase storage and collect the image URL from firebase and that is what I will be sending -ignoring the image bit for now.. any advice on how to send the form data...am pretty green on that – DanielSmurts Nov 17 '19 at 16:40

0 Answers0