Fill field after wp_remote_post()

My form has multiple pages and I want to send the data from the first two pages to a remote server for processing and then post the response to the field on the third page
field on the third page.

Sending using

add_filter('gform_validation_1', 'custom_validation_function', 10, 4);

function custom_validation_function($validation_result, $form, $entry, $field_values)
{
    // Überprüfen, ob Feld 1 und Feld 7 gefüllt sind
    $field1_value = rgpost('input_1'); // Ersetze 1 mit der tatsächlichen ID von Feld 1
    $field7_value = rgpost('input_7'); // Ersetze 7 mit der tatsächlichen ID von Feld 7

    if (!empty($field1_value) && !empty($field7_value)) {
        // Die Felder 1 und 7 sind gefüllt, fahre fort

        // Daten vorbereiten, die an den entfernten Server gesendet werden sollen
        $data_to_send = array(
            'field1' => $field1_value,
            'field7' => $field7_value,
        );

        // URL des entfernten Servers, an den der AJAX-Request gesendet wird
        $remote_server_url = 'https://********.php';

        // AJAX-Request senden
        $response = wp_remote_post($remote_server_url, array(
            'body' => json_encode($data_to_send),
            'headers' => array('Content-Type' => 'application/json'),
        ));

		// Serverantwort verarbeiten und in das Feld mit Parametername "server_response_Gliederung" einfügen
		if (!is_wp_error($response)) {
			// Erfolgreicher Request an den entfernten Server
			// Die Serverantwort verarbeiten und in das Feld mit Parametername "server_response" einfügen
			$response_body = wp_remote_retrieve_body($response);
			error_log('Serverantwort: ' . $response_body);
			$response_data = json_decode($response_body, true);
			error_log('Serverantwort: ' . $response_data['field8']);
			if (isset($response_data['field8'])) {
				// Füge die Serverantwort in das Feld mit Parametername "server_response_Gliederung" ein
				$validation_result['form']['server_response_Gliederung'] = $response_data['field8'];
			}
		}
    }

    return $validation_result;
}

works perfectly at this point. The response is written to debug.log - so the data is available in general.

What’s not working is to fill the text area on page 3 (ID8) with the content that I get back from the server.

Does anyone have an idea how this can be realized or where my error might lie?

THANK YOU !!!

You can store your response in the $_POST, like this:

$_POST['input_8'] = $response_body;

That will populate field 8 in the form with the $response_body.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.