Page 1 of 1

assign a value to a form field in PHP

Posted: Fri Jun 06, 2014 8:58 pm
by WRBailey
I am just starting to use nuBuilder, but I have looked a lot of forum entries and the documentation and tried quite a few things to assign a value to a form field. I don't want to assign the value to the table. I want the save button to do that. I just want to assign a value to a field in a form in the Before Save PHP code area.

From what I can see the most pleasing syntax would be: #field_to_set# = $value; This doesn't work for me.
I tried to exit PHP to use html or JavaScript and reenter php. This didn't scan. So most of the examples I found in JavaScript need to be invoked differently.

I can see that there is a way to store JavaScript code and then use it. But I don't follow how to make it work for me.

Can someone show me the PHP code to assign a value to a form field. I have the value in the variable $newnumber and the name of the form field is powerlevel.

I know this will be simple, but I feel like I'm going in circles and need help.

Thank you.

Re: assign a value to a form field in PHP

Posted: Mon Jun 09, 2014 12:42 am
by massiws
WRBailey,
in Custom Code -> Before Save you can manage field values in this way:

Code: Select all

$newnumber = "#my_test_field#";
// ... make what you want ...
nuDebug($newnumber); 
But, if you want to assign a value to a field, you're working on client side, so you should use Javascript/jQuery:
  • create an object on your form where to store your data:
    Example text field
    Example text field
    test_field.png (6.49 KiB) Viewed 3031 times
  • in Curstom Code -> Javascript insert nuOnSave() function to manage field value like you want:

    Code: Select all

    function nuOnSave() {
        var data = $( '#my_test_field' ).val();  // To read the value in the field
        alert(data);
    
        var newvalue = 'my new value';
        $( '#my_test_field' ).val( newvalue );   // To assign a new value
    
    
        // ...
        // This function must return TRUE (to save data) or FALSE 
    }
Hope this helps,
Max

Re: assign a value to a form field in PHP

Posted: Mon Jun 09, 2014 8:03 am
by WRBailey
Thank you, Max. I saw the JavaScript code to do the job. I saw a $_POST too. Because all my work to derive the number is in PHP, I wonder if there is a way in nuBuilder to pass a value to javascript. In some of my web pages I have supplied content to forms by invoking PHP, echoing the varialbe and exiting PHP. Is there something like this you can suggest. I don't mind using JavaScript or html or whatever client-side language is available, I just need to know how to get my variable's value to it. Any ideas?

Thanks again.

Bill Bailey

Re: assign a value to a form field in PHP

Posted: Mon Jun 09, 2014 6:46 pm
by massiws
Bill,
WRBailey wrote:Because all my work to derive the number is in PHP, I wonder if there is a way in nuBuilder to pass a value to javascript. In some of my web pages I have supplied content to forms by invoking PHP, echoing the varialbe and exiting PHP. Is there something like this you can suggest.
you can use AJAX to calculate and return the value to display in your field: the PHP script can be called on some Javascript event.
On Before Save, you should only perform data validation.

Max