0

I know this had been disputed a lot and the short answer would be that I can't simply pass a javascript variable to a php variable in the same file. So instead here would be what I want to try: My user inputs a code, I send it to a php file by POST. There I check if the code matches a code from my database and I post again a boolean from there. Then in my first file, I tell the user whether the code is correct or not. Can this be achieved this way? I am a webdev newbie and I am trying to learn.

12
  • 1
    Yes you can do that with ajax post. Commented May 9, 2013 at 17:08
  • I know this had been disputed a lot - What has? Commented May 9, 2013 at 17:08
  • Short answer would be that you can't? Ever heard of AJAX? Commented May 9, 2013 at 17:09
  • you can achieve this using AJAX. Commented May 9, 2013 at 17:09
  • I was refering to passing javascript variable to php variable Commented May 9, 2013 at 17:10

3 Answers 3

1

To pass a value from the client side to the server you can either send it on the URL or as a post variable.

This can be accomplished easily with ajax.

I recommend using jquery. example:

$.get("http://example.com/?var1=value&var2=othervalue", function (data) {
  // your response
});
Sign up to request clarification or add additional context in comments.

Comments

0

jQuery:

$.get( url, { userinput: value }, function( response ) {
    if( response.status ) alert( "Matches found" );
    else alert( "No matches" );
}

javascript:

function get( url ) {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url,  false );
  xhr.send();
  return xhr.responseText;
}

var response = JSON.parse( get( url ) );
if( response.status ) alert( "Matches found" );
else alert( "No matches" );

php:

header( 'Content-type: text/json' );
if( get_matches( $_GET['userinput'] ) ) exit( '{ "status": true }' );
else exit( '{ "status": false }' );

1 Comment

For the total novitiate: the $.get() syntax requires jQuery.
0

You can post that code through AJAX to your server, have your server return a boolean, and then output a message to your user, this is quite common.

Common implementations of this logic include autosuggest, username validity verifications, simple turn on / turn off interfaces, etc.

Workflow:

  • User inputs code
  • Javascript sends AJAX request to server
  • Server verifies code and returns boolean
  • Javascript reads boolean, generates HTML and appends it to the document
  • User reads output

Edit: Even though i advice you to try doing it with pure javascript first (for educational reasons), you should use jQuery or other equivalent framework if you are on a schedule.

4 Comments

Yeah- well that's what I'm trying to do. The part I don't know how to do is how to read the boolean with javascript
Oh, you want the actual code? im sure you can find a truckload of tutorials out there for this, as it is a trivial ajax application
No, I did not expect the actual code. Just a hint on how to read the bool with js
Assuming your server returns 1, 0, true or false: $.post(url, data, function (result) { if (result) {/*true*/} else {/*true*/} });

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.