Jump to content
The ORIGINAL FileMaker Community - Forum - Online Business Apps & Software Forum
mjhnson24

PHP API Form Data

Recommended Posts

mjhnson24

[

post in abeyance pending editing by the author

]

Share this post


Link to post
Share on other sites
doughemi

OK,  Do you want to pull the id field from some number of records, display those on a form, retrieve a value input from a user for each id , and return the values to the database?

Or do you want to pull the current values from the database and allow the user to change them and return them to the database?

Why do you want to create new records? Having the same data (the id) in two different records is very bad database structure. You probably  need to have a "value" field in the same record as the id.

Please describe the real-world problem you are trying to solve. Your description of how you're trying to solve it makes no sense.

Share this post


Link to post
Share on other sites
mjhnson24

Well we dont want to have the record im pulling data from updated by customer. We want them to enter data and it add to a new record in a new table and we control if the original record gets updated. Theres scripting that runs when the original record is updated and its quit lengthy. We want full control of what happens. We dont want customers updating real records. Thats the reasoning behind creating a record in a new table with the same id as the original but it would be a foreign key though in that new table. That record in the new table will be deleted after we finish it. 

 

But basically im wanting to pull two fields from original table. RecordID field and location field into a list of records in PHP. The list will show two columns (Location and form input). But somehow i need the record ID in there so when client fills out input field for each record in the list, they click submit and it basically loops through list and creating a new record for each one in the list in a new table with the value from input field and record ID i pulled for each record. But i dont know where i should put the record id i get from first query to be able to take that as well to the new table record.I need that record id for the new table so we know which record it will relate to in filemaker. 

 

Its basically a level check for scent customers. They input how many milliliters they have for each of their machines in the list. They click submit and it takes the input value and the record id for each record and adds it to a new table. Then we go and look at it and determine if its ok for us to add it to the original record or not or if something is wrong with the numbers compared to what we currently have then we can call customer and verify. Thats the reason we need to do it this way.

Share this post


Link to post
Share on other sites
doughemi

In very broad terms,  the sequence you have to follow is:

Create your found set of records in FM and fetch them with the API commands

/* PHP Code to send data back to temporary table in database. 
	This actually happens last in the sequence, but must be first in the code.
    When form is submitted, all the inputs are stored in a $_POST array, so first
    we have to see if $_POST is set. If not, we have to get the data from the main db.*/
//
//API connection code to database goes here
//
If isset($_POST){
	$addRecord = $fm->newAddCommand("TempTableLayout");
	foreach($_POST as $id=>$amount){
    	$addRecord->setField("tempIDfield", $id);
        $addRecord->setField("tempAmountField", $amount);
     }
     $added = $addRecord->execute();
//
// error trapping code goes here
//

 } else {   
	/*PHP Code to get info from database*/
	$custRecords = $fm->newFindCommand("phpLayout");
	$custRecords->addFindCriterion("YourCustomerIDField", $desiredCustID);
	$result =  $custRecords->execute();
//
// error trapping code goes here
//
	$supplies = $result->getRecords();
	$qty = 0;
	$array = array;
	foreach($supplies as $item=>$qty){
		$item = $supplies->getField("ID");
		$array[$item] = $qty;
	}

Create your html form with the php :

<form id="orderform" method="post" action="<? php echo __FILE__;?>" >
  <?php
	foreach ($array as $key=>$value){
		echo '<p><label> ' . $key . '</label><input type="text"  name=" . $key . '</input></p>'
	}
  ?>
</form>

The API  is layout-centric. To make it as efficient as possible, create dedicated layouts with only the fields you need to interact with  (get or set).

Edited by doughemi
missing comment open in line after "} else {"

Share this post


Link to post
Share on other sites
mjhnson24

So heres what i have. 

 

php code to get list of records:

<form action="/clientLevelChecks/submit.php" method="POST">
<?php
        echo '<table>
            <thead>
                <tr>
                    <th>Location</th>
                    <th>Unit Serial #</th>
                    <th>Service ID</th>
                </tr>
            </thead>
            <tbody>';
 
        #LOOP THROUGH ARRAY AND DISPLAY RECORDS IN LIST
        foreach ($records as $record) {
            echo '<tr>';
            echo '<td>' . $record->getField('UnitLocation') . '</td>';
            echo '<td>' . $record->getField('UnitSerialNumber') . '</td>';
            echo '<td>' . $record->getField('_REC_ID') . '</td>';
            echo '<td>level: <input type="text" name="LevelAmount"></td>';
            echo '<td><input type="hidden" name="id" value="' . $record->getField('_REC_ID') . '"></td>';
            echo '</tr>';
        }
    }
    echo '<tr><td><input type="submit" value="Submit"></td></tr>';
    echo '</tbody></table>';
?>
    </form>

 

The code above gives me the html code on browser below.

<form action="/clientLevelChecks/submit.php" method="POST">
<table>
<thead>
<tr>
<th>Location</th>
<th>Unit Serial #</th>
<th>Service ID</th>
</tr>
</thead>
<tbody>
<tr>
<td>Marks Office</td>
<td>XUnit-C1066-S046</td>
<td>S046</td>
<td>level: <input type="text" name="LevelAmount" value="460"></td>
<td><input type="hidden" name="id" value="S046"></td>
</tr>
<tr>
<td>no where</td>
<td>A15000000</td>
<td>S047</td>
<td>level: <input type="text" name="LevelAmount" value="470"></td>
<td><input type="hidden" name="id" value="S047"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</tbody>
</table>
</form>

 

I hard coded in the code above the values for the two level amount fields for test purposes. But they would enter in whatever value they needed. When i submit i get the below array. That array only gives me one but if it gives me two then it wont really separate each record in the list in its own subarray. The one below is only getting one record not two like its listing in the browser.

Array
(
    [LevelAmount] => 
    [id] => S047
)

 

 

I need it to be something like the aray below so i can loop through each item and get the LevelAmount and ID and send to filemaker for each one. So as below i got to item1 and that will be one record i send to FM temp table and create a temp record and then i loop through to the next record and get item2 etc. Basically like a nested array. 

 

Array
(
    (
        [LevelAmount] => 460
        [id] => S046
    ),
    (
        [LevelAmount] => 470
        [id] => S047
    )
)

 

Hopefully this helps

 

Share this post


Link to post
Share on other sites
mjhnson24

right now i dont have any of the addrecord content like you have in your php code above but i will once i see that im getting the data like i need it or it needs to be. Once i do that ill crate the addRecord stuff and after that ill put in some code to redirect them to a different page stating thanks for submitting etc. or just echo out text on that page stating the same thing or whatever.

Share this post


Link to post
Share on other sites
mjhnson24

I did add the section of adding the record and as per the one array item it does give me it does add that to my table but in the 

$addRecord->setField("tempIDfield", $id);

it adds the word "id" in the tempIDField and it adds the actual id it gets from that into the LevelAmount field instead of putting the levelAmount in the level amount field and the id in the id field like it should.

 

I guess because it sees the array below as two separate records and not one record

 

Array
(
    [LevelAmount] => 470
    [id] => S047
)

 

when. it should be something like 

Array
(
    (
        [LevelAmount] => 460
        [id] => S046
    ),
    (
        [LevelAmount] => 470
        [id] => S047
    )
)
Edited by mjhnson24

Share this post


Link to post
Share on other sites
mjhnson24

changed the foreach on add to below and it adds the correct data to correct fields but still its only showing one record and array isn't formatted to a multi record array.

 

foreach($_POST as $id=>$amount) {
                $addRecord->setField("ServiceJoinID", $_POST['id']);
                $addRecord->setField("LevelAmount", $_POST['LevelAmount']);
            }

Share this post


Link to post
Share on other sites
doughemi

You're not showing me where  $records comes from and what it looks like. Put the code

echo '<pre>';
print_r($records);
echo ''</pre>';

at the beginning of your first screen shot. It should show both of your test records. If it does not, (and I expect it won't) and shows only one item in the array, then the rest of your code appears to work properly. Please advise what it does show

How did you define $records?

Share this post


Link to post
Share on other sites
mjhnson24

if you go to the url you will see a list of 5 records. if you fill in data for all 5 records in the level column then click submit then the array that shows up for the $_POST array only shows 1 record and thats the record that gets entered into filemaker after submitting. After submitting i should or need it so that array position 0 is one record and shows S1147 and the level 147 amount, array position 1 should show S1148 and the level 148 as the amount etc etc for array positions 2, 3, ad 4 which covers all 5 records at the url above.

 

https://secure.ohmserv.com/clientLevelChecks/?id=C1887

Share this post


Link to post
Share on other sites
doughemi

Clicking the link  shows me a blank page with "Error: No active service records found."

Share this post


Link to post
Share on other sites
doughemi
Quote

After submitting


Array
(
    [LevelAmount] => 470
    [id] => S047
)

How are you submitting that? The response you show for the print_r has nothing to do with that.  My suggestion should have output array elements like ohcj12570=>250 per your original format.

Share this post


Link to post
Share on other sites
mjhnson24
10 minutes ago, doughemi said:

Clicking the link  shows me a blank page with "Error: No active service records found."

Ok it will show records now. Had to change password info. I posted data earlier that had user and password info in it by accident and had to report the two posts so i can get them removed . and had to change passwords just incase. Didn't realize it till after i posted them.

 

Thanks doughemo by the way for staying with me! I really appreciate it alot. 

 

if i can figure out how to get all 5 records in that list you see and send each one to FM as a new record with just the LevelAmount and the ServiceID you see which is also a hidden inout field then i will be golden i think. But for some reason its only pulling the last record in the POST array.

Share this post


Link to post
Share on other sites
mjhnson24
25 minutes ago, doughemi said:

How are you submitting that? The response you show for the print_r has nothing to do with that.  My suggestion should have output array elements like ohcj12570=>250 per your original format.

the ohcj12570 is actually S047 etc. the ServiceID starts with an S instead of ohcj like originally posted. Below is the form that i have. The foreach loop thats wrapped around the for tags is whats gives me the original list. Then the customer will inout the levels for each location. When they click submit it performs the code wrapped around the $_SERVER variable. If i get data in $_POST then it prints it using the print_r($_POST) so i can see the data in the $_POST ARRAY and check to see if im getting all inout fields with serviceID as its own record. 

 

This code is a very dumbdown version of what i have but this is in essence of what makes me get the records in a list first. Then the client can fill out the input form value field and then submit. When they submit again i wanna take the amount they fill out and the serviceid and add to FM temp table for each records listed.

<?php
#OTHER CODE HERE TO QUERY FM TO GET THE RECORDS FIRST
 
if ($_SERVER["REQUEST_METHOD"] == "POST"){
 
$addRecord = $fm->newAddCommand('phplayout');
foreach($_POST as $id=>$amount) {
$addRecord->setField("####", $_POST['id']);
$addRecord->setField("####", $_POST['LevelAmount']);
}
$added = $addRecord->execute();
 
#ERROR TRAPPING CODE HERE
 
#THIS STEP IS ONLY GIVING ME THE ONE RECORD IN ARRAY IVE POSTED PREVIOUSLY
print_r($_POST);
 
#IF FORM NOT SUBMITTED YET
}
 
#HAVE CODE ABOVE THIS THAT WILL TAKE DATA FROM SUBMIT FORM AND ADD THE DATA FROM FORM BELOW TO FILEMAKER TEMP TABLE
$records = $findresult->getRecords();
?>
<form action="" method="POST">
<?php
        echo '<table>
            <thead>
                <tr>
                    <th>Location</th>
                    <th>Unit Serial #</th>
                    <th>Service ID</th>
                    <th>Level</th>
                </tr>
            </thead>
            <tbody>';
 
        #LOOP THROUGH $records variable from variable above to list all records it got from my first and original table.
        foreach ($records as $record) {
            echo '<tr>';
            echo '<td>' . $record->getField('Location') . '</td>';
            echo '<td>' . $record->getField('SerialNumber') . '</td>';
            echo '<td>' . $record->getField(' RECID') . '</td>';
            echo '<td><input type="text" name="LevelAmount"></td>';
            echo '<td><input type="hidden" name="id" value="' . $record->getField('RECID') . '"></td>';
            echo '</tr>';
        }
 
    echo '<tr><td><input type="submit" value="Submit"></td></tr>';
    echo '</tbody></table>';
        }
    }
?>
</form>

 

Each of the 5 rows has two inout fields, one of which is hidden to get the serviceID, the other input field is what the client enters the levels into. Each row needs to be separated in the array as its own. 

 

Basically kinda like the array below where you have a multi dimensional array with array[0] has id of S1147 with level amount 147,

array[1] = id of S1148 and level amount 148 and so on. 

 

 

Array
(
    (
        [LevelAmount] => 147
        [id] => S1147
    ),
    (
        [LevelAmount] => 148
        [id] => S1148
    ),
    (
        [LevelAmount] => 149
        [id] => S1149
    ),,
    (
        [LevelAmount] => 150
        [id] => S1150
    ),,
    (
        [LevelAmount] => 151
        [id] => S1151
    ),

)

 

Now if i submit the form that you see in the browser i can get an array like this and loop through in essence creating a record for each one in my temp table.

Share this post


Link to post
Share on other sites
mjhnson24

As per this screenshot, This is what i get when i query the database to get a list of records the customer needs to input level amounts at. This data comes from a table that will only be read from and thats it. When clients fills in values as seen below, they click submit. Currently when you do it only puts the last record in the $_POST array and thats the one thats added to FM. It should loop through and take the first one with S1147 and 147 Level and add to tempTable, then go to next and add S1148 and 148 level as a 2nd record in tempTable and so on for all records listed. 

 

1184203195_ScreenShot2018-12-06at5_09_30PM.png.a64ada7c2284980916ab1fb40e1a2dc5.png

 

This is the raw html code of the page as it is displayed in the above image.

<form action="" method="POST">
<table>
<thead>
<tr>
<th>Location</th>
<th>Unit Serial #</th>
<th>Service ID</th>
<th>Level</th>
</tr>
</thead>
<tbody>
<tr>
<td>Main Casino Entrance</td>
<td>CR5E180019B</td>
<td>S1147</td>
<td><input type="text" name="LevelAmount"></td>
<td><input type="hidden" name="id" value="S1147"></td>
</tr>
<tr>
<td>Registration Entrance (Next to DeLorean)</td>
<td>CR5E180030B</td>
<td>S1148</td>
<td><input type="text" name="LevelAmount"></td>
<td><input type="hidden" name="id" value="S1148"></td>
</tr>
<tr>
<td>East Entrance (Next to Elvis Car)</td>
<td>CR5E180022B</td>
<td>S1149</td>
<td><input type="text" name="LevelAmount"></td>
<td><input type="hidden" name="id" value="S1149"></td>
</tr>
<tr>
<td>Hotel Elevator Bathroom - Mens</td>
<td>CS3G170147</td>
<td>S1150</td>
<td><input type="text" name="LevelAmount"></td>
<td><input type="hidden" name="id" value="S1150"></td>
</tr>
<tr>
<td>Hotel Elevator Bathroom - Womens</td>
<td>CS3G170159</td>
<td>S1151</td>
<td><input type="text" name="LevelAmount"></td>
<td><input type="hidden" name="id" value="S1151"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</tbody>
</table>
</form>
Edited by mjhnson24

Share this post


Link to post
Share on other sites
doughemi

You keep skipping a step: getting the data from the database to the PHP script. This has to be done before creating a web page. This is what I am asking. Please define this because it is the point at which you are not getting all the inputs, and until I can see how you are trying to accomplish this, your last half dozen or more posts cannot be answered.

Share this post


Link to post
Share on other sites
mjhnson24

I dont know what you mean by me skipping a step. I type in the url in the browser. It queries the services table for the records that the client needs. It displays the records as in the screenshot i posted in the above post and i posted the html it shows in the browser or it. The Client types in the level amounts for each record. They click submit. currently when i click submit it only submits the data in the last record to the new tempTable i created in filemaker. It doesn't add the other 4. If you want i can post my phone number and your more than welcome to call me if that helps any better.

 

The code below is how im getting data from filemaker to the php page initially and displaying in a list form like you saw. Minus doing the normal newFindCommand steps like normal to find a record in filemaker php for api. The foreach step below loops through the $records variable and displays them on the php page. Again all that works great and displays what i need The part that dont is when clicking the submit button it dont take all the records in the list one by one and add the serviceID and level amount back to filemaker new tempTable. Thats the part the when i click submit and print the $_POST array to the page its only giving me the data for the last record not the other 4 with it. Thats the only other way i can explain it or know what your needing. theres nothing else more than that that i know i can give you.

and as you can see i got a hidden input field in the foreach loop thats captures the record id i need. The only pieces of data i need from that is the data from the two input fields for each record in the list.

$records = $findresult->getRecords();
?>
<form action="" method="POST">
<?php
        echo '<table>
            <thead>
                <tr>
                    <th>Location</th>
                    <th>Unit Serial #</th>
                    <th>Service ID</th>
                    <th>Level</th>
                </tr>
            </thead>
            <tbody>';

 

        #LOOP THROUGH $records variable from variable above to list all records it got from my first and original table.
        foreach ($records as $record) {
            echo '<tr>';
            echo '<td>' . $record->getField('Location') . '</td>';
            echo '<td>' . $record->getField('SerialNumber') . '</td>';
            echo '<td>' . $record->getField(' RECID') . '</td>';
            echo '<td><input type="text" name="LevelAmount"></td>';
            echo '<td><input type="hidden" name="id" value="' . $record->getField('RECID') . '"></td>';
            echo '</tr>';
        }

 

    echo '<tr><td><input type="submit" value="Submit"></td></tr>';
    echo '</tbody></table>';
        }
    }
?>
</form>

Share this post


Link to post
Share on other sites
doughemi

Your file only puts the record data in HTML. The API works in PHP. You have to pull the data from the database into PHP and then use the PHP to populate the HTML. When your user fills out the form, you have to save that POST data in a PHP array, (because HTML doesn't have arrays).  See the attached files. The database username and password are "user", and you have to put your own server name in the PHP file.

These files only show the download part. You should be able to extrapolate the upload part  once you understand the download.

Uncomment the echo and print_r() statements to see the arrays. 

Archive.zip

Share this post


Link to post
Share on other sites
mjhnson24

I dont know what you mean by me skipping a step. I type in the url in the browser. It queries the services table for the records that the client needs. It displays the records as in the screenshot i posted in the above post and i posted the html it shows in the browser or it. The Client types in the level amounts for each record. They click submit. currently when i click submit it only submits the data in the last record to the new tempTable i created in filemaker. It doesn't add the other 4. If you want i can post my phone number and your more than welcome to call me if that helps any better.

 

The code below is how im getting data from filemaker to the php page initially and displaying in a list form like you saw. Minus doing the normal newFindCommand steps like normal to find a record in filemaker php for api. The foreach step below loops through the $records variable and displays them on the php page. Again all that works great and displays what i need The part that dont is when clicking the submit button it dont take all the records in the list one by one and add the serviceID and level amount back to filemaker new tempTable. Thats the part the when i click submit and print the $_POST array to the page its only giving me the data for the last record not the other 4 with it. Thats the only other way i can explain it or know what your needing. theres nothing else more than that that i know i can give you.

and as you can see i got a hidden input field in the foreach loop thats captures the record id i need. The only pieces of data i need from that is the data from the two input fields for each record in the list.

#Here is how im doing the find that queries the database initially to list the records to the php page initially before the client enteres any data into the input fields.

 

$findrequest = $fm->newFindCommand('php layout here');

$findrequest->addFindCriterion('ClientID', $ClientID');

$findrequest->addFindCriterion('status', 'ACTIVE');

$findresult = $findrequest->execute();

$records = $findresult->getRecords();
?>
<form action="" method="POST">
<?php
        echo '<table>
            <thead>
                <tr>
                    <th>Location</th>
                    <th>Unit Serial #</th>
                    <th>Service ID</th>
                    <th>Level</th>
                </tr>
            </thead>
            <tbody>';

 

        #LOOP THROUGH $records variable from variable above to list all records it got from my first and original table.
        foreach ($records as $record) {
            echo '<tr>';
            echo '<td>' . $record->getField('Location') . '</td>';
            echo '<td>' . $record->getField('SerialNumber') . '</td>';
            echo '<td>' . $record->getField(' RECID') . '</td>';
            echo '<td><input type="text" name="LevelAmount"></td>';
            echo '<td><input type="hidden" name="id" value="' . $record->getField('RECID') . '"></td>';
            echo '</tr>';
        }

 

    echo '<tr><td><input type="submit" value="Submit"></td></tr>';
    echo '</tbody></table>';
        }
    }
?>
</form>

Share this post


Link to post
Share on other sites
mjhnson24
2 minutes ago, doughemi said:

Your file only puts the record data in HTML. The API works in PHP. You have to pull the data from the database into PHP and then use the PHP to populate the HTML. When your user fills out the form, you have to save that POST data in a PHP array, (because HTML doesn't have arrays).  See the attached files. The database username and password are "user", and you have to put your own server name in the PHP file.

These files only show the download part. You should be able to extrapolate the upload part  once you understand the download.

Uncomment the echo and print_r() statements to see the arrays. 

Archive.zip

yea i know i gotta use php to talk to filemaker to query the database to be able to display data in html format in the browser. That again i already have working. I can query the database in the file. Thats how those 5 records got displayed in the browser from the beginning. Look at the previous post of mine above yours jsut now and you will see the code from the initial find command to displaying the records on the php page. Again that code above works in regards to displaying records in the browser.

Share this post


Link to post
Share on other sites
mjhnson24

again when i click submit i look at the $_POST array to see what its giving me and its only giving me the data for the last record only and i need it in a way so that i get in this case with the test records i showed above i need the levelAmount and serviceID for each record so i need to loop through the post array and send the levelAMount and serviceID and create a new record in filemaker with that data for each of the 5 records but it will only do the last one cause the post array is only showing me the last record not all 5. If you wanna do a teamviewer screenshare or something tomorrow when i get back to work tomorrow 8am CST then i can do that and show you physically what i got.

 

I know how to get data form filemaker and send it to filemaker and create a new record etc what im having issues with is the data thats listed in a list view and clicking submit its only giving me the last record in the post array and i need two pieces of data for each of the 5 records and send them to filemaker to create a record.

Share this post


Link to post
Share on other sites
This thread is quite old. Please start a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.




×
×
  • Create New...

Important Information

Terms of Use