0

how to take a Value from a javascript variable, to be used as a php input variable via a button, not a submit button because the submit button cannot generate sequences more than once at the same time? I have tried this method but I still can't get the output ("x") from js using the button. this is my first code with javascript I tried as best I could because I tried to use break but it didn't work then i put x-- to freze it.

this javascript work like i want, but still can't get value "x" from button value

Code:

<title>Test-2019</title>
<!-- x += 1 -->
<!-- Java Script -->
<script> 
x = 0;
function f_Next()
{
    x++;
    document.getElementById("Next").value = x;
    if(x == 5) {
        x--;
    }
}
</script>
<!-- HTML -->
<form method="post" action="#">
    <input type="button" value="Soal" id="Next" onClick="f_Next()">
    <!-- 
    <input type="hidden" value="0" id="Next">
    -->
</form>
<!-- PHP -->
<?
$a = array("str1", "str2", "str3", "str4", "str5");
if (isset($_POST['soal']))
{
    $va = $_POST['soal'];
    print $a[$va];
}
?>

on delphi I always use this method and I also tried to implement it but it didn't work so I tried making it with java script.

Delphi Example Code:

//global var
const
    a : array[0..4] of string = ('str1', 'str2', 'str3',  'str4', 'str5');
 var
    g : integer;
    ...
    ...
    //on create
    g := 0;
    ...
    ...
    //on button click
    g := g + 1;
 if g = 5 then
    g = 0;
    edit1.text := IntToStr(g);

  //or like this example 2
   button1.tag := button1.tag + 1;
if button1.tag = 5 then
   button1.tag := 0;       
   edit1.text := IntToStr(button1.tag);

php code was i tried, like this

function fNext();
{
    $x = 0;
    if ($x == 5)
    {
        break;
    }
    x += 1;
}

i'm sorry if i included pascal/delphi code, cz maybe you can help me to convert it into php.

Thank you.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • You cannot parse value from form without action url or ajax request –  Jun 05 '19 at 08:38
  • `php` is a server-side language which is executed on the server before any `client-side` languages are executed such as `javascript` so you will need to send the data (in some way) from `javascript` to `php` which will require a `form` or some use of `ajax` – NewToJS Jun 05 '19 at 08:40
  • what is the javascript function supposed to do exactly? Currently it will increment x to 5 then set it to 4 ad-infinitum. Is it supposed to send x to the server on each click? – Professor Abronsius Jun 05 '19 at 08:42
  • each click value (x) to represent the displacement of the array branch parent selection that I will develop further, each array has a boolean true / false option branch, if the condition x = 5 (end of the problem) then I will calculate true / false using a percentage then submit result – Klepon Racun Jun 05 '19 at 16:37

2 Answers2

0

Not really sure what you are trying to do here, BUT if you want the value of an <input> element to get to PHP it HAS TO HAVE A name attribute!

So change your input to

<input name="Next" type="button" value="Soal" id="Next" onClick="f_Next()">`
//     ^^^^^^^^^^^  added this 

Now you need to address it properly in the $_POST array, so if we call the element Next as in name="Next" we should use $_POST['Next'] and not $_POST['soal'] as soal was the value attribute which by the way the javascript should have overwritten with 1 or 2 etc

$a = array("str1", "str2", "str3", "str4", "str5");

if (isset($_POST['Next']))
{
    $va = $_POST['Next'];
    print $a[$va];
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

I'm not 100% sure I grasped the intent of the question but I hope this might be more or less the desired outcome?

Each click of the button increments the counter x and will send this value to the PHP script. The PHP script responds with the value from the array appropriate to the value of x

The ajax callback increments the variable and does other, really interesting, things.

<?php
    $a = array("str1", "str2", "str3", "str4", "str5");
    if (isset($_POST['soal'])){
        $va = $_POST['soal'];

        $response=( array_key_exists( $va, $a ) ) ? $a[ $va ] : 'error';
        exit( $response );
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Test-2019</title>
        <script>

            document.addEventListener('DOMContentLoaded', ()=>{

                let x=0;

                const ajax=function(payload,callback){
                    let xhr=new XMLHttpRequest();
                        xhr.onreadystatechange=function(){
                            if( this.status==200 && this.readyState==4 )callback.call( this, this.response );
                        }
                        xhr.open('POST', location.href, true );
                        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                        xhr.send( payload );
                }
                const callback=function(r){
                    alert(r);
                    x++;
                    if( x==5 )x=0;
                }
                document.querySelector( 'form input[ type="button" ]' ).addEventListener('click', function(e){
                    ajax.call( this, 'soal=' + x, callback )
                });
            });

        </script>
    </head>
    <body>
        <form method='post'>
            <input type='button' value='Soal' />
        </form>
    </body>
</html>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • I have tried your code, right, I mean it's like that, to be honest, I was just yesterday, trying to learn java script and that's just about increment & decrement. i'm sorry, i don't understand about ajax, because i usually only use php, html & mysql. and I'm confused to modify your code to change the (message dialog / message box) to be displayed with echo / print – Klepon Racun Jun 05 '19 at 17:13
  • AJAX is simply a small part ( sort of ) of Javascript and it is inextricably linked with HTML, CSS and PHP in many websites - these combined technologies form much of the interwebs. The `callback` function can do anything you want, within reason, when processing the response from the server.... you clearly have an idea in mind as to how you wish to use this methodology judging by the PHP code ( array ) but have not clearly explained the end goal - though if my code helped, great - if not refine your question. – Professor Abronsius Jun 05 '19 at 19:19