0

I have a form where the users submit a text. Here is the code:

$form['reason'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#size' => '200',
            '#attributes' =>array('placeholder' => t('*Reason')),
            '#prefix' => '<div>'
          );


    $form['newsletter_unsubscribe_submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
        '#attributes'=> array(
            'class' => ['btn', 'btn-primary']
        ),
        '#ajax' => array(
            'callback' => 'newsletter_unsubscribe_quiz_submit_driven_callback_share',
            'method' => 'replace',
            'effect' => 'fade',
        ),
        '#prefix' => '<div class="regular newsletter_unsubscribe_button">',
        '#suffix' => '</div></div>');


    return $form;

I managed to get the input of the user on a variable using $_values = $form_state['values'];
Also, I have some other variables to read from the current path and get some values.

    $_path = $_SERVER['HTTP_HOST'] . request_uri();

    preg_match('~id=(.*?)&~', $_path, $id);

    $first = $id[1];
    $second = substr($_path, -5);

where the first is the user id and the second is another variable that is unique per user.
My only problem is that I can't pass the $first and $second variables through the callback, and have them along with $reason $_reason = filter_xss($_values['reason']);
So, in my callback I want to be able to get the variables $first,$second with the $reason I already have. Now I have the $_path variables above my form, in the same function.
Then I want to perform an update query on MySQL, something like

UPDATE users SET reason=$reason
WHERE first=$first AND second=$second

where the column users has only the columns first,second and reason . Is this possible? Thanks in advance!

EDIT
I actually managed to do the first part and get the variables. I created two form inputs and I give them default values, the two variables I want to get, and I will hide them afterwards.
I still haven't figured out how to update MySQL though, so all answers are welcome!

2

I actually managed to do it! For anyone that may have a similar problem this is what I did.
First of all, I took the url parameters using parse_url and did the following

$parts =  parse_url($_path);
parse_str($parts['query'], $query);
$first = $query['first'];
$second = $query['second'];

,then put those variables as form default values, that I will hide using css:

$form['user_id'] = array (
         '#type' => 'textfield',
         '#title' => t('User id'),
         '#description' => t('user id'),
         '#default_value' => $first,
);

Then, on my callback function I take those variables, for example:

$_first = filter_xss($_values['first']);

and then I used db_update:

$num_updated = db_update('unsubscribed_users')
    ->fields(array(
        'reason' => $_reason,
      ))
    ->condition('first', $_first , '=')
    ->condition('second', $_second , '=')
    ->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.