LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2021, 05:30 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 361

Rep: Reputation: 9
Questions Re: multiple INSERT statements to server PHP


I have some questions regarding coding of multiple INSERT statements going to my server.

My code is based on an example I found in fiddle. Here is the code and my questions

Code below will run.

PHP Code:

Code:
<?php
// Fri May 28, 2021 14:30 
// multiAdd.php
// to serve multiInsert.html

set_include_path( '../include' );
error_reporting (E_ALL ^ E_NOTICE);

include('myPhpFunctions.inc'); 

//myprint($_POST); //bye("multiAdd.php Line 12"); Shows $_POST array - OK

print_r($_POST);

echo json_encode($_POST); // Returns Json data - OK
	 
 ?>
HTML Code:

Code:
<!DOCTYPE html>

<!-- multiInsert.html -->
<!-- Adapted from http://jsfiddle.net/sperske/8e5ae/10/ -->

<html>
<head><title>testing multi insert function multiInsert.html 28May2021</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	
<script  type="text/javascript" src="../jquery/jquery-2.1.4.js"></script>

<style> 

tbody#template {
    display: none;
}

body  {
	font-family: roman, 'times new roman', times, serif;
	font-size: 14pt;
	margin: auto;
	text-align: center;
	background-color: #6795c8;
}

input {
	font-size: 12pt;
	color: blue;
}

</style>	
	
</head> <!--===================  End <head>  ===================== -->
  
<body>


<table id="tabledata">
    <thead>
        <th>Id</th>
        <th>Vendor</th>
        <th>OrderNo</th>
        <th>DrugNo</th>
        <th>Description</th>
        <th>Cost</th>
        <th>Refills</th>
    </thead>
    <tbody id="input"></tbody>
    <tbody id="template">
        <tr>
            <td>
                <input name="Id" type="text" />
            </td>
            <td>
                <input name="Vendor" type="text" />
            </td>
            <td>
                <input name="OrderNo" type="text" />
            </td>
            <td>
                <input name="DrugNo" type="text" />
            </td>
            <td>
                <input name="Description" type="text" />
            </td>
            <td>
                <input name="Cost" type="text" />
            </td>
            <td>
                <input name="Refills" type="text" />
            </td>
            
       </tr>
    </tbody>
</table>
<button id="ActionAddRow">Add Row</button>
<button id="ActionSubmit">Submit</button>


<script type="text/javascript">

$(function () {
    var addInputRow = function () {
        $('#input').append($('#template').html());
    };

    addInputRow();
    $('#ActionAddRow').on('click', addInputRow);
    $('#ActionSubmit').on('click', function () {
        var mydata = $('#input tr').map(function () {
            var values = {};
            $('input', $(this)).each(function () {
                if (this.type === 'checkbox') {        // org fiddle code
                    values[this.name] = this.checked;  //   ditto
                } else {
                    values[this.name] = this.value;
                }
            });
            return values; // to what ? - is this for ".get() ?" 
        })
        .get();    //What does this do - does not work without this
        $.post('multiAdd.php', {
        			mydata,
            //json: JSON.stringify(data),  Original fiddle code
            //delay: 1                     ditto 
        })
 
/*			$.ajax({
				type: "POST",
				url: "multiAdd.php",    This does not work - how to fix ??
				data: mdata,
				dataType: "json",
			}) // ajax
*/			       
        .done(function (response) {
            alert("POST success");
            console.log(response);
        });
    });
});

</script>

</body>
</html>

Last edited by pizzipie; 05-28-2021 at 05:34 PM. Reason: tweaks
 
Old 05-29-2021, 12:31 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,616
Blog Entries: 11

Rep: Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655
I do not fiddle so not sure how that works, but if possible you may do better to ask the person that posted the example.

Code:
    $('#ActionAddRow').on('click', addInputRow);
    $('#ActionSubmit').on('click', function () {
        var mydata = $('#input tr').map(function () {
            var values = {};
            $('input', $(this)).each(function () {
                if (this.type === 'checkbox') {        // org fiddle code
                    values[this.name] = this.checked;  //   ditto
                } else {
                    values[this.name] = this.value;
                }
            });
            return values; // to what ? - is this for ".get() ?" 
        })
        .get();    //What does this do - does not work without this
...
It has been a while since I worked with jquery, but each of those chained functions must return the jquery object (I forget what they call it but that is the $ object), which allows you to chain any of its member methods together like $().on().get(); for example.

Simply looking at the scope in which the return values; statement occurs will tell you that it is the argument for the map() method (Hint: Just find the opening curly brace corresponding to the closing curly brace following the return).

.get() is just the next method in the chain.

Quote:
This does not work - how to fix ??
By now you surely know that is not a useful problem description. Please elaborate.

Also, your title suggests an SQL INSERT related problem which has nothing to do with your example. Please consider changing the title to reflect the actual question being asked to help others choose questions for which they are best able to offer help.

Last edited by astrogeek; 05-29-2021 at 01:04 PM.
 
Old 06-04-2021, 11:57 AM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 361

Original Poster
Rep: Reputation: 9
Thanks Astrogeek,

I finally found some answers to my questions. For those interested check out the following:

Quote:
Iterating over jQuery and non-jQuery Objects
https://learn.jquery.com/using-jquery-core/iterating/
There is more to this article but specifically:

Quote:
Code:
$( "li" ).map( function(index, element) {
    return this.id;
}).get();
Notice the .get() chained at the end. .map() actually returns a jQuery-wrapped collection, even if we return strings out of the callback. We need to use the argument-less version of .get() in order to return a basic JavaScript array that we can work with. To concatenate into a string, we can chain the plain JS .join() array method after .get().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assistance with multiple php if statements genderbender Programming 1 07-06-2011 04:08 PM
[SOLVED] Ctrl-Insert and Shift-Insert don't work val3xiv Linux - General 2 06-30-2011 05:17 PM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 04:37 AM
SQL: multiple WHERE statements elvijs Programming 2 07-02-2007 09:17 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration