0

I have a simple form like this:

<form>
  <label>Subject</label>  
  <input id="subjec_input" name="subject_input" type="text">
  <label>Message Body</label>             
  <textarea id="body_input" name="body_input"></textarea>
  <button id="submit_btn" name="submit_btn">Submit</button>
</form>

I want to take the input from the form and put subject_input into a and body_input into b

<?php send_msg( 2, 'a', 'b' );?>

At the moment I have this code which fires the function on page load but I want it to only fire when the user clicks submit.

I have tried using get but have not been successful

3
  • 5
    Show us your unsuccessful attempt. This is really basic PHP. Commented Sep 26, 2017 at 11:58
  • 3
    We are always glad to help and support new coders but you need to help yourself first. :-) After doing more research if you have a problem post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this. Commented Sep 26, 2017 at 11:59
  • use $_GET['subject_input'] in place of a and $_GET['body_input'] in place of b Commented Sep 26, 2017 at 11:59

4 Answers 4

2

if you are using get request then you can do

<?php
$body_input=$_GET['body_input'];
$subject_input=$_GET['subject_input'];

send_msg( 2,$body_input, $subject_input);

if you are using Post Request then your html form tag must define method="POST"

  <?php
    $body_input=isset($_POST['body_input'])?$_POST['body_input']:'';
    $subject_input=isset($_POST['subject_input'])?$_POST['subject_input']:'';

    send_msg( 2,$body_input, $subject_input);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I used post because get was changing the URL. Then I used this: stackoverflow.com/a/26380651/1045794 to hide the post. :)
0

First give your form a method like GET/POST

<form action="welcome.php" method="get">

Then in your PHP code access them with super globals like :

$a = $_GET['body_input']

Then pass those to your function

Comments

0
    if(isset($_POST['submit_btn'])){
    echo $subject_input = $_POST["subject_input"];
     echo $body_input = $_POST["body_input"];
send_msg( 2,$body_input, $subject_input);

 }

You can use it like this

Comments

0

You need to use:

 if(isset($_POST['submit_btn'])){

 $a = $_POST['subjec_input'];
 $b = $_POST['body_input'];  

 //Run your function/code.

 }

Change your form to method "post" :

 <form action="" method="post">

Also, edit your button to be of type "submit" :

 <button type="submit" id="submit_btn" name="submit_btn">Submit</button>

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.