0

I want to insert a checkbox to a HTML form which is stored in a PHP string variable $form:

$form = "
  <form action='/action_page.php'>
     First name:<br>
     <input type='text' value='Mickey'><br>
     Last name:<br>
     <input type='text' name='lastname' value='Mouse'><br>
     <input type='submit' value='Submit'>
  </form>";

$checkbox = "<input type='checkbox' name='agree' required> I agree!";

Context: Wordpress add_filter callback gets the form and should add the checkbox at the end of the form.

Thanks!

2 Answers 2

1

I think you want to insert the checkbox in form but before submit button. You can use this

$form = "
  <form action='/action_page.php'>
     First name:<br>
     <input type='text' value='Mickey'><br>
     Last name:<br>
     <input type='text' name='lastname' value='Mouse'><br>
     <input type='submit' value='Submit'>
  </form>";

$checkbox = "<input type='checkbox' name='agree' required> I agree!";
$form = str_replace("<input type='submit'", $checkbox."<input type='submit'", $form);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a regular expression:

Don't forget to put the ending tag 'form' after variable $checkbox

preg_replace('/<\/form>/', $checkbox.'</form>', $form);

Probably should have better ways to do it, but I don't know wordpress architecture to sugest a better solution.

Link of preg_replace: https://www.php.net/manual/en/function.preg-replace.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.