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

redditor
  • 4,196
  • 1
  • 19
  • 40
  • 5
    Show us your unsuccessful attempt. [This is really basic PHP.](http://php.net/manual/en/tutorial.forms.php) – Script47 Sep 26 '17 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**](https://meta.stackoverflow.com/q/261592/1011527) 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](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Sep 26 '17 at 11:59
  • use `$_GET['subject_input']` in place of `a` and `$_GET['body_input']` in place of `b` – mega6382 Sep 26 '17 at 11:59

4 Answers4

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);
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
  • Thanks. I used `post` because `get` was changing the URL. Then I used this: https://stackoverflow.com/a/26380651/1045794 to hide the post. :) – redditor Sep 26 '17 at 13:34
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>
cmprogram
  • 1,854
  • 2
  • 13
  • 25
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

Vikas Gautam
  • 997
  • 7
  • 23
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

RohanS404
  • 41
  • 1
  • 8