views:

85

answers:

1

I am working within the confines of a php based CMS like system, which defines custom form fields that can be used when making a new page.

I have two tables in my database, one called people, which has three fields, firstname, middlename and lastname, and a second table called orders, which also has firstname, middlename and lastname fields, as well as product, quantity and cost.

I am using one of the custom fields, inputSmartSearch, which works somewhat like google suggest. As I type letters an SQl query is performed returning results that match, concatenated together.

For example, if I put 'ba' as the input, one result might be 'banner, david, bruce', and after I select it 'banner, david, bruce' is now the text content in the inputSmartSearch field.

in the people table, david is in the firstname field, bruce in the middlename field and banner in the lastname field.

What I want to do is find out how to access the text that is present in the inputsmartsearch field(i.e. whatever the user has selected), and break it again up to 3 parts, so I can insert the firstname into orders.firstname, middlename into orders.middlename and lastname into orders.lastname, as well as the input from the other simpler fields on the site into their relevant fields in the orders table.

There may well be a simpler solution, such as using a unique primary guestid and just inserting that into the orders table, but even then my current problem is still the same.

How do I access the content of the inputsmartsearch field after it is full?

The HTML generated by a search is as follows:

<div id="searchBox-chooseguests" class="smartSearchBox" style="top: 199px; left: 42px; width: 190px;">
<div id="SBHeader-chooseguests" class="SBHeader">
<button tabindex="4000" class="graphicButtons buttonX" id="SBCloseButton-chooseguests" type="button">
<span>close</span>
</button></div>
<div id="SBResults-chooseguests" class="SBResults"> 
<a tabindex="4000" id="SB-:08490ea8-d654-1c91-cb82-00d44a4b093b" class="SBSearchItems SBSI-chooseguests " href="#">
<span class="SBMain">Sherlock Homes</span>
<span class="SBExtra">333333334</span>
</a>
</div>
<div id="SBFooter">
</div>
</div>

Since that generated HTML is dynamic, I don´t know how much use it will be.

The generated HTML for the actual field never changes, even when filled with a value...so I don´t know how to access what it holds.

It is as follows:

    <p class="big"><label for="ds-chooseguests">Choose Guest</label>
<br>
<input type="hidden" value=":08490ea8-d654-1c91-cb82-00d44a4b093b" id="chooseguests" name="chooseguests">
            <input type="hidden" value="1" id="sff-chooseguests">
            <input type="hidden" value="17" id="sdbid-chooseguests">
            <input type="text" value="" class="inputSmartSearch important" title="Use % for wildcard searches." id="ds-chooseguests" name="ds-chooseguests" autocomplete="off"></p>

I am limited in what javascript I can use(I think), and must use fields defined by the CMS system(I think...but not sure....perhaps no requirement to do so...)

Some pointers in the general direction here would be amazing.

edit:

This is the _POST data that gets sent:

Array ( [chooseguests] => :08490ea8-d654-1c91-cb82-00d44a4b093b [ds-chooseguests] => Holmes, Sherlock [chooseproducts] => 75c72a6a-83d9-11df-951a-fa9c1ec271f2 [ds-chooseproducts] => Corona [quantity] => 2 [type] => cash [receiptno] => 7 [createdby] => [creationdate] => [command] => save [modifiedby] => [cancelclick] => 0 [modifieddate] => [uuid] => :4402add3-b884-43e6-04ad-c76d92ee465b [id] => )

And this is my current form, based on the template linked above:

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

    include("../../include/session.php");
    include("include/tables.php");
    include("include/fields.php");

    //if you need to ovveride the phpbmsTable class make sure to include the modules file
    include("include/sales.php");


    //If the addedit page will be accessd directly from a page other than the
    // basic search results page, you may want to grab and pass the previous URL
    //with the following code

    //===================================================
    if(!isset($_GET["backurl"]))
        $backurl = NULL;
    else{
        $backurl = $_GET["backurl"];
        if(isset($_GET["refid"]))
            $backurl .= "?refid=".$_GET["refid"];
    }
    //===================================================

    //      $thetable = new phpbmsTable($db,[table definition id]);
    // Next we process the form (if submitted) and
    // return the current record as an array ($therecord)
    // or if this is a new record, it returns the defaults
    $thetable = new sales($db, "tbld:490cf2d1-1c72-7b99-461d-b1b8e68553c4");
    $therecord = $thetable->processAddEditPage();

    if(isset($therecord["phpbmsStatus"]))
        $statusmessage = $therecord["phpbmsStatus"];

    $pageTitle = "Sales";

    // Next, we set up to include any
    // additional css or javascript files we will be using
    //  This does nto include any field-type specific js (like datepicker)
    // as they are automatically icluded when you define the special fields you
    // will be using below.
    $phpbms->cssIncludes[] = "pages/menus.css";
    $phpbms->jsIncludes[] = "modules/base/javascript/menu.js";
    $phpbms->jsIncludes[] = "modules/micro hospitality/javascript/checkpaid.js";

    // DEPRECIATED:
    // if you need to define a body onlload function, do so with the phpbms property
    $phpbms->onload[] = "initializePage()";

print_r($_POST);
    // Next we need to define any special fields that will be used in the form
    // A list of field objects (with documentation)is available in the /include/fields.php
    // file.

    // We need to define them here in the head
    // so that any necessay javascript is loaded appropriately.

        $theform = new phpbmsForm();
        //if you need to set specific form vaiables (like enctype, or extra onsubmit
        // you can set those form properties here.

        // for each field we will use, create the field object and add it to
        // the forms list.

        $theinput = new inputSmartSearch($db, "chooseguests", "Choose Guests",NULL, "Choose Guest", TRUE, NULL, NULL, TRUE, $required=true);
        $theinput->setAttribute("class","important");
        $theform->addField($theinput);

        $theinput = new inputSmartSearch($db, "chooseproducts", "Choose Product",$therecord["product"], "Choose Product", TRUE, NULL, NULL, TRUE, $required=true);
        $theinput->setAttribute("class","important");
        $theform->addField($theinput);

        $theinput = new inputField("quantity",$therecord["quantity"],"Quantity",true, NULL, 1);
        $theinput->setAttribute("class","important");
        $theform->addField($theinput);

        $theinput = new inputBasicList("type",$therecord["paymenttype"],array("Cash"=>"cash","Credit"=>"credit"), "Payment Type");
        $theinput->setAttribute("class","important");
        $theform->addField($theinput);

        $theinput = new inputCheckbox("paid", $therecord["paid"], "Paid");
        $theform->addField($theinput);

        $theinput = new inputField("receiptno",$therecord["receiptno"],"Receipt No",true, NULL, 10);
        $theinput->setAttribute("class","important");
        $theform->addField($theinput);

        $thetable->getCustomFieldInfo();
        $theform->prepCustomFields($db, $thetable->customFieldsQueryResult, $therecord);
        $theform->jsMerge();

    include("header.php");

?><div class="bodyline">

    <?php $theform->startForm($pageTitle)?>

    <div id="leftSideDiv">
        <fieldset>
            <legend><label for="S">Sales</label></legend>

            <p class="big"><?php $theform->showField("chooseguests"); ?></p>
            <p class="big"><?php $theform->showField("chooseproducts"); ?></p>
            <p class="big"><?php $theform->showField("quantity"); ?></p>
            <p class="big"><?php $theform->showField("type"); ?></p>
            <p class="big"><?php $theform->showField("paid"); ?></p>
            <p class="big"><?php $theform->showField("receiptno"); ?></p>


        </fieldset>
    </div>
    <?php
        //Last, we show the create/modifiy with the bottom save and cancel buttons
        // and then close the form.
        $theform->showGeneralInfo($phpbms,$therecord);
        $theform->endForm();
    ?>
</div>
<?php include("footer.php");?>

I have trimmed it as much as I can, and only left in those comments that I think may be useful for relating to the problem/question.

A: 

So without seeing all the working code and knowing what limits you have I would approach it with something like this. It looks like you can get the contents of the selection by just reading the value from the input text field.

var selection = document.getElementById("ds-chooseguests").value

I would imagine that this has the contents of the selection.

Then you can just do a split on the value:

var selection_parts = selection.split(',');

Now you can access each part with an index

selectionParts[0] = "banner ";
selectionParts[1] = "david ";
selectionParts[2] = "bruce ";

I tried to keep it using basic javascript with no external libraries. Hopefully this is what you had in mind.

spinon
Hi, thanks for your answer, and sorry for my ignorance which leads to this follow up question: How would I access document.getElementById("ds-chooseguests").value through php, or even selectionParts[0] through php(using your example) so that I could insert it back into the database? I don´t want to paste the code just because it is quite large, and I don´t know that one part out of context would be useful
Jacob
Well you wouldn't use that code from php. Those were if you wanted to get them in javascript.If you wanted to get them in php you should be able to just get that information from the post data: $selection = $_POST["ds-chooseguests"]Then just do the split as normal:$selection_parts = explode(",", $selection);Then index like was mentioned before:$selection_parts[0] = "banner ";$selection_parts[1] = "david ";$selection_parts[2] = "bruce ";
spinon
I´m not sure that any POSt data is sent at all :| - it might be sent when I click on save form, and I suppose I could modify it that way. If it will help, I am using phpBMS, and an example of the form template is here: http://www.phpbms.org/browser/trunk/phpbms/modules/sample/sampletable_addedit.php
Jacob
Well the post data would depend on how the form is being submitted. If the action of the form is post then you could get it that way. If it is get then just change the $_POST to $_GET.Without seeing where and when you are trying to use this I'm not sure what else I can offer.
spinon
OK, POSt data is sent, like so: Array ( [chooseguests] => :08490ea8-d654-1c91-cb82-00d44a4b093b [ds-chooseguests] => Holmes, Sherlock [chooseproducts] => 75c72a6a-83d9-11df-951a-fa9c1ec271f2 [ds-chooseproducts] => Corona [quantity] => 2 [type] => cash [receiptno] => 7 [createdby] => [creationdate] => [command] => save [modifiedby] => [cancelclick] => 0 [modifieddate] => [uuid] => :4402add3-b884-43e6-04ad-c76d92ee465b [id] => ) - can I read in and manipulate the contents of [chooseguests] before function uses it to insert into the db?
Jacob
I would imagine you could but without seeing all of this code in context I can't know for sure. I would need to see if there is a place to modify before it gets persisted.
spinon
OK, I have edited my answer to include my current form.
Jacob
Is it possible perhaps to modify the POSt data directly, or perhaps with JS before it gets sent?
Jacob
so both options you mentioned are possible. How would you plan on writing this data back to the database. If you want to do on the js you could add a few extra hidden fields to the page and then when you parse the data from the field you could put each one of the values into a hidden field and read with the post data.Though since you have all this data in the array it would seem that you could just do from php. I'm not seeing where in the code above are you having the data in the array persisted to the database. Are you able to add fields to that array that would automatically get picked up?
spinon
Your last question as actually what I am unclear on. I think a lot is hidden in the function used at the end. I know I can override things to a certain level with my include file, such as the example here: http://www.phpbms.org/browser/trunk/phpbms/modules/sample/include/sampletable.phpCheck if _POST exists and if it does working with it at the very start of the form may be sufficient?- The only POST request is to the form itself, so it must be handled some way within?
Jacob
Ok so looking at the page you sent it appears that an array of names and values are sent to a method that converts it into a statement to update the database. So do any of the fields in the array you posted earlier match the fields in your database? If so then what I would do is parse the data like I showed you earlier and then try adding a few values to this array and see if they are saved.$array["firstname"] = "";$array["lastname"] = "";$array["middlename"] = "";That might work for you. If not I think you might want to try on a forum specific to that cms system. Hopefully this works!
spinon
Hi, first of all, thanks so much foryour help, this is really stumping me! - the fields do match the field namesin my table, but there is a problem - the quanity input field has the input quanity, and this is not changed and saves to the database fine, however the chooseproducts inputfield with same id gets changed to ds-chooseproducts, and so the contents of that fields does not get inserted at all. - When you say add a few values to the array, can you clarify and give an example that relates back to the form I posted? i.e. do you mean add values to the _POST array?
Jacob
Also, unfortunately I can´t ask the authors for help...they charge for support and the forum is dead, many people asking for help. I really do need help with this, and could pay you a very small amount foryour time if you were interested....
Jacob
well if you want there is a contact form on my website and you can send me a message through there and then we can chat offline. I'll take a look and if it is something small that we can figure out don't worry about paying me.
spinon
Hi spinon, I tried to use the form on your website, but it didnt work, perhaps because of my email address which is [email protected] - if you shoot me a mail ill get back right back to you.
Jacob
Nice. I will check it out and see. That is funny. :PI'll send you a message right now.
spinon
Shameless request: maybe a little answer mark on here wouldn't be bad :P
spinon
But of course :) - just a note, I haven´t received any mail....is yahoo acting up perhaps?
Jacob