0

I have code like this

<select onchange="getval(this);"> 
     <option value="">Select</option>
     <option value="1">one</option>
     <option value="2">two</option>
     <option value="3">three</option>
</select>

And my script like this

function getval(sel){
//alert(sel.value);
    <?php $sql = "SELECT * FROM tbl_table WHERE id="?>+(sel.value)
}

I want select data from table where id values from javascript but I don't know how to write php in javascript tag and how to add javascript's variable (sel.value) after PHP

How can I correct this syntax?

9
  • 1
    Use ajax, you can't do it like this. You can't assign javascript variable or value to PHP. Commented Oct 22, 2014 at 5:03
  • 1
    fuction like that'll work. Commented Oct 22, 2014 at 5:04
  • this is a bad idea. Don't do like this. Better use ajax other than use PHP with in javascript Commented Oct 22, 2014 at 5:04
  • @Fred-ii- what u mean ? Commented Oct 22, 2014 at 5:05
  • The word is function, not fuction. Commented Oct 22, 2014 at 5:06

3 Answers 3

5

May this will help you. You can use with ajax...

mainfile.php

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>

function getval(x){
    var data_arr="id="+x;  
    $.ajax({
        type:"POST",
        url:"anotherfile.php",
        data:data_arr,
         success: function(response){
            // here your response will come now you have to deside how to maintain this response...
         }

    });
}
</script>

<select onchange="getval(this.value);"> 
     <option value="">Select</option>
     <option value="1">one</option>
     <option value="2">two</option>
     <option value="3">three</option>
</select>

and your another file which contain ajax code is here...

anotherfile.php

<?php 
mysql_connect("hostname","username","password");
mysql_select_db("database_name");
$sql = "SELECT * FROM tbl_table WHERE id=".$_REQUEST['id'];
while($row = mysql_fetch_array($sql)){
    // here your out put data code...
}
// and finally write all data in echo statement they will return as response

hope you got it...

Sign up to request clarification or add additional context in comments.

Comments

3

Php is server side scripting language and JavaScript is client side language so it seems to be impossible! Further Explanation.

PHP is a programming language. It is often used for server side programming, but has uses in general programming too.

JavaScript is a programming language. It is the only language that has a decent level of native support for running in a browser.

Similarity:

  1. Both languages are almost exclusively for the web and were created specifically for web use in the mid 90s.
  2. Syntax styles are both based on C
  3. Until only recently with PHP going full OO, both languages were not officially OO languages
  4. Both languages are platform independent (compiler or ‘runtime environment’ is required though)

Differences:

  1. PHP is server side while JavaScript is client side.
  2. PHP makes use of classes while JavaScript only has constructors and functions.
  3. JavaScript is used for a lot of visual effects and enhancements for web GUIs.
  4. Users are able to disable all JavaScript while browsing the internet due to its client-side compilation.

Solution:

AJAX = Asynchronous JavaScript and XML.

AJAX is not a new programming language, but a new way to use existing standards.

AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page.

1 Comment

thats why they added the XMLHttpRequest API to circumvent this limitation.
1

If I understood you correctly, you cannot write PHP in this way. PHP stays on the server, and never gets shown on the page. You need to either POST the page or use AJAX. The most basic way is to use a 'Submit' button and handle the result with PHP. Look at this basic example: http://www.tizag.com/phpT/forms.php?

1 Comment

Please don't use w3schools as a resources. See this: meta.stackexchange.com/questions/87678/….

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.