0

I have a problem about which I am very confused. I have a select box with s dynamically generated using a mysqli query:

$result = mysqli_query($db, "SELECT * FROM `users` WHERE `user_id` > 0");
echo '<html><form name="contacts" method="post"><select name="contacts"><option value="Contact list" onchange="func()">Contact List</option>';
while($row = $result->fetch_assoc()){
    echo '<option value = '.$row['user_name'].'>'.$row['user_name'] . '</option>';
}
echo '</select></form>';

I am completely new to AJAX, but I need to use jquery and ajax to pass the this.value variable to a php variable for use in a later query.

Here is my script (most of which was found online):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
$("#contacts").change(function() {
    //get the selected value
    var selectedValue = this.value;

    //make the ajax call
    $.ajax({
        url: 'function.php',
        type: 'POST',
        data: {option : selectedValue},
        success: function() {
            console.log("Data sent!");
        }
    });
});
</script>

Now, when I click a value in the select box, nothing happens. There are no warnings or errors, etc.

Please help me.

p.s. function.php does exist. It is just a simple echo for now (for testing purposes)

UPDATE: HERE IS FUNCION.PHP:

<?php
/*$val = $_REQUEST['selectedValue'];
echo $val;*/

function function(){
$val = $_REQUEST['selectedValue'];
echo $val;
}
?>

UPDATE: Thank you everyone for all your help. I have now got it to work in that the network section of chrome inspect shows the function.php being requested however I still don't get the echo (I used external .js files to get it to work). My J query function is also successful (the success function echoes into the console)

7
  • Pop open your console (F12 usually) - and check the Network tab when you change an option -- see if anything is happening. Commented Mar 13, 2014 at 16:20
  • you should echo it in php file man Commented Mar 13, 2014 at 16:21
  • @tymeJV yep, I get this: Uncaught ReferenceError: func is not defined Commented Mar 13, 2014 at 16:30
  • @user3411896 You should remove that event handler, see my answer. Commented Mar 13, 2014 at 16:32
  • 1
    You cannot call your function function and you need to call it for it to do something... Commented Mar 13, 2014 at 16:42

5 Answers 5

6

Your select box has no ID and you are watching the change event of $("#contacts").

Change:

echo '<html><form name="contacts" method="post"><select name="contacts"><option value="Contact list" onchange="func()">Contact List</option>';

to:

echo '<html><form name="contacts" method="post"><select name="contacts" id="contacts"><option value="Contact list">Contact List</option>';
                                                                        ^^^^^^^^^^^^^ here

You also only need one event handler, so I have removed the inline one which doesn't seem to do anything anyway.

Edit: If the select is created using ajax as well, you need event delegation:

$("body").on('change', '#contacts', function() {
   ^^^^ for example

Edit 2: Your variable is called $_REQUEST['option'] and not $_REQUEST['selectedValue']. You are also not calling your -badly named - function so you will not get any output from php except from an error like Parse error: syntax error, unexpected 'function' ....

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

5 Comments

Thanks. Obviously, that doesn't immediately solve the problem, but thanks anyway
@user3411896 How is it obvious that it does not solve the problem? It should. And if it doesn't, is the select created using ajax as well? In that case you need event delegation: $("body").on('change', '#contacts', function() {
The created select is just using php and mysqli, sorry that I didn't think it would solve the problem (I am a n00b)
@user3411896 Is the html well-formed? If the names contain ' characters they will break your html so you should (normally always...) use htmlspecialchars($row['user_name']) instead of just $row['user_name'].
OK, I will bear that in mind, however the users do not contain special characters
3

Call onchange function in select tag as below

echo '<form name="contacts" method="post"><select name="contacts" onchange="func(this.value)"><option value="Contact list">Contact List</option></form>';

Javascript src should be in head of the html page.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

Add the above one in head of html. Update javacript as below

As onchange function is called in the select tag itself, following is enough

<script>

function func(selectedValue)
 {
    //make the ajax call
    $.ajax({
        url: 'function.php',
        type: 'POST',
        data: {option : selectedValue},
        success: function() {
            console.log("Data sent!");
        }
    });
}
</script>

Updated php: If you must want to get value from function, you must call it. Otherwise, simply, you can make as below

    <?php
    if($_REQUEST['option'])
    {
    $val=$_REQUEST['option'];
echo $val;
     }
    ?>

10 Comments

Thank you, I have copied and pasted this, however there is still no visible change when I select a different option
Try to add alert(selectedValue); in the success function
If you are able to get selectedValue in the alert box, there should be a problem with server side php. Have you copied above updated php?
yes, I still have the error: Uncaught ReferenceError: func is not defined index.php?page=inbox:53 onchange(on the line of PHP)
Have you changed the select tag like this <select name="contacts" onchange="func(this.value)">
|
0

In .php file, receive it first-

$val = $_REQUEST['selectedValue'];
echo $val;

1 Comment

I have copied that code in instead of the original echo, still nothing happens
0

set an id attribute in your php code for the select tag and

please don't use the same value for the name attribute in form and select tags !!

Comments

0

just change your function to a 'body'.on, and give your elements an id of 'contacts'

$("body").on('change', '#contacts', function() {
    //get the selected value
    var selectedValue = $(this).val();

    //make the ajax call
    $.ajax({
        url: 'function.php',
        type: 'POST',
        data: {option : selectedValue},
        success: function() {
            console.log("Data sent!");
        }
    });
});

1 Comment

yah, I guess @jeroen 's answer is the same thing.

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.