Get html value to pass to php withini the file

How can I get an HTML value to send to PHP. I would like to avoid using a form. For example I have an input:

<input name="input" id="input">Input</input>

while in PHP:

$input = $_POST['input'] --> but didn't work

or

$input = $_GET['input'] --> still didn't work

I know that I will be able to get it using form then action="another file" but I want it within a file. Please help. Thank you.

If you simply want to use js to append the input value in url variable then you can use js function as follows;

client.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" id="txt">
    <a onclick="append()" id="anch" href="server.php?text">Send value</a>
</body>
</html>
<script>
    function append()
    {
        txt=document.getElementById('txt');
        anch=document.getElementById('anch');
        anch.href=anch.href+"="+txt.value;
    }
</script>

server.php

$variable = $_GET["input"];
echo $variable;

Output on localhost:

After clicking send value:-

Update: I just read the end of your question. If you want to show the value of input on same page/file then you can use following code;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" id="txt">
    <?php
        if(isset($_GET['text']))
        {
            $text=$_GET['text'];
            echo "<a onclick='append()' id='anch' href='?text=$text'>Enter Value</a>";
        }
        else
        {
            echo "<a onclick='append()' id='anch' href='?text'>Enter Value</a>";
        }
    ?>

    <br>
    <?php
        if(isset($_GET['text']))
        echo "<div id='values'>".$_GET['text']."<div>";
    ?>
</body>
</html>
<script>
    function append()
    {
        txt=document.getElementById('txt');
        anch=document.getElementById('anch');
        if(anch.href.match(/=/)=="=")
        anch.href=anch.href+txt.value+"<br>";
        else
        anch.href=anch.href+"="+txt.value+"<br>";
    }
</script>

Output:

Check output on phpFiddle

PHP and HTML - Manual, What encoding/decoding do I need when I pass a value through a form/URL? Note: In fact you are faking a HTML GET request, therefore it's necessary to The PHP variable with unpredictable value can originate from some file, as an� PHP file_get_html - 30 examples found. These are the top rated real world PHP examples of file_get_html extracted from open source projects. You can rate examples to help us improve the quality of examples.

It seems that the way to go is to use the GET method.

You will need to access your page with the get parameters included like this:

http://localhost/index.php?input=VALUE_HERE

And in your php file:

$variable = $_GET["input"];
echo $variable;

But if you insist to use inline html elements to get data, you need to use javascript for that like this for jquery:

alert("This is the value of the input" + $("#input").val());

Variables From External Sources - Manual, So, for example, http://www.example.com/test.php?id=3 contains GET data So, for a shopping cart application you may want to keep a counter and pass this along. Naturally the 'value' of our buttons in the HTML form that we want to display POSTs with multipart/form-data content type, since the file upload handler has� Action Attributes: specifies the PHP script file location for processing when it is submitted. If no script file location is specified, the browser submits the form by using the current PHP script file location ( the self-script in which the form is being called ). Method Attributes: specifies what type of method the form will use to send the data.

Don't use this <input name="input" id="input">Input</input> Use this <input name="input" id="input">

PHP: $_POST, With the first example you'd have to do string parsing / regexes to get the correct values out so they can be married with other data in your app whereas with the The name attribute on your input controls is what $_POST uses to index the data Superglobals � $GLOBALS � $_SERVER � $_GET � $_POST � $_ FILES� How do I display my mysql table column headers in my php/html output? php,html,mysql,table,data. Note: You can just make a single file out of it to achieve your wanted output Use mysql_real_escape_string() to sanitize the passed-on value to prevent SQL injections You should use mysqli_* instead of the deprecated mysql_* API Form them in a

Send, pass, get variables with PHP, Form, HTML & JavaScript code, <form name="myform4"> <input type="hidden" name="formvar" value="100"> </ form> <script type="text/javascript"> jsvar = document.myform4.formvar.value;� We tell the PHP interpreter to start a session by defining session_start() at the beginning of every PHP file we want session to happen. Then we access the session variables using the $_SESSION['variable-name'] method. PHP code with HTML: <?php session_start (); //Put session start at the beginning of the file?> <!

Passing PHP form variables from one page to other, This is in continuation of the PHP multi-page order form article. <input type=" hidden" name="email_address" value="<?php echo $_POST['email_address']; ? >� Thanks! Here is my complete working script orderform.php -> input processorder.php ->display & edit once clicked edit back to orderform.php with autofill

How to pass variables and data from PHP to JavaScript , We can pass data from PHP to JavaScript in two ways depending on the situation . Here, we just take input by statically or dynamically and pass it to resulting output and then pass it to the variable x and then the value of x is printed. in PHP cookie and retrieve it from JavaScript using document.cookie. PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. Before reading these faqs, it's important you learn how to retrieve variables from external sources. The manual page on this topic includes many examples as well. What encoding/decoding do I need when I pass a

Comments
  • Probably the GET method will work, you just need to append the parameter to the url like: /?input=VALUE_HERE
  • could you give me an example? i don't get what you mean
  • View my answer below
  • what trigger to get your input value? when user finish input or what? it is exist submit button?
  • @Rin_asdfghjkl i updated my answer see if it helps
  • I believe that u used this localhost/index.php?input=VALUE_HERE to pass data to another page. I've done this in updating data, but would like to depend on ONE file only.
  • regarding the javascript, i dont know how i can get the value from jquery to php
  • Do you want to create a form and submit it without reloading the page?