0

On my website I have a login form which submits some data (e.g. email address) to a PHP function AND starts the function running, which creates a user.

I want to pass some additional information from the page where the form is t

This is the data I want to send through the form:

<?php
    $variable = "Your Name Here"
?>

This is the form itself:

<form action='http://dev3.benefacto.org/wp-admin/admin-ajax.php' method='GET'>
<b>Work E-Mail: </b><input type='text' name='email' id='email' placeholder='Enter Your Work Email' />
<input type='text' style='display: none' name='action' id='action' value='logged_in_check2' />
<input type='text' name='variable' id='variable' value=$variable />
&nbsp;&nbsp;<input type='submit' value='Next'  class='cta' />

Any for arguments sake let's say the PHP function is

function simple_name_return()
{
echo 'Hello'
$variable = $_GET["variable"];
echo $variable;
}

add_action("wp_ajax_nopriv_simple_name_return", "simple_name_return");
add_action("wp_ajax_simple_name_return", "simple_name_return");

I think this might be helpful but I am completely new to JavaScript so a bit stumped: Pass Javascript variable to PHP via ajax

Any thoughts much appreciated.

Linz Darlington
  • 515
  • 1
  • 10
  • 25

2 Answers2

0

I answered a very similar question here.

Basically you need to pass the variable via POST/GET, check the result and respond. To check the email you would do something like this:

emailcheck.php

<?php

// Assume success by default
$result = true;

if (!filter_var($_GET['email'], FILTER_VALID_EMAIL)) {
    $result = false;
}

// Other checks, such as domain here, set $result to false if check fails

// Throw error if email isn't valid so it can be picked up by $.ajax
if (!$result) {
    http_response_code(500);
}

JS (requires jquery)

function isValidFormEmail()
{
    // Perform ajax call to emailcheck.php
    $.ajax({
        url: '/emailcheck.php?email=' + $('#email').val(), 
        error: function() {
            $('#custom-email-error').show();
        },
        success: function(data) {
            // Submit form
            $('form').submit();
        }
    });

    // Prevent form from submitting on click
    return false;
}

This should work with your HTML as it is currently providing the action URL moves to the next action.

sjdaws
  • 3,466
  • 16
  • 20
  • Thanks @sjdaws, but I threw curve ball leaving the thing about isValidFormEmail() in there. That works but I excluded the code. What I'm failing to do is pass the PHP variable $variable, through the form so it can be used in the PHP Function that is called by the form. Can you help on that pls? Thx in advance. – Linz Darlington Jun 09 '17 at 12:27
  • When you submit the form what is inside `$_GET`? Looking at your edit it should have a key of variable with the value in it. Try `var_dump($_GET);exit;` to see what is being passed from the form. – sjdaws Jun 09 '17 at 12:29
  • In the end I used this: `` And then added this to the form. Works well, looks a bit clunky. `value=''` – Linz Darlington Jun 09 '17 at 17:08
-1

Since you are submitting form to a page you need first receive the variable in admin-ajax.php then you can pass it to a function.