0

I m searching something of easy. I must pass value from a form html to a file PHP by jquery. I try this code with zero result. If someone can say me where i m mistaking. Thx

for JQUERY

$('#Save').click(function(){

    var realvalues = new Array();//storing the selected values inside an array
    $('#Privilege[] :selected').each(function(i, selected) {
        realvalues[i] = $(selected).val();
    });

    $.ajax({
        type: "POST",
        url: "test5.php",
        data: {"Privilege[]": realvalues},
        success:function(data){
                $("#subscrres").html(data)
            }
    });
});

For HTML

<form method="post">
<select id="Privilege[]" multiple>
<option value="yahoo">yahoo</option>
<option value="chrome">chrome</option>
<option value="mozilla">mozilla</option>
</select>
<input type="button" id="Save" Value="SEND"/>

For PHP. Content file test5.php

if(isset($_POST['Privilege'])){
$myvar  =$_POST['Privilege'];
foreach($_POST['Privilege'] as $one)
echo $one."<br/>";

}

I don't receive nothing on PHP. Someone can help me ?

6
  • Add a var_dump($_POST) to the top of your PHP and see what you're receiving. Commented Aug 7, 2020 at 16:59
  • 1
    You're sending "Privilege[]" but you're checking for isset($_POST['Privilege']) Commented Aug 7, 2020 at 17:01
  • Ok, thx Jaj and i m agree with you, but i 've no yet resolved, sure i've furthmore problem...i suppose. Commented Aug 7, 2020 at 17:08
  • 1
    The post is missing the </form> but you probably left it out. Try adding the "name" attribute to the <select>. Commented Aug 7, 2020 at 17:28
  • Thx for your help Funk Forty Niner, i 've add name to the selector, the end of form is already present, but nothing is changed....will be became a real rebus :) Commented Aug 7, 2020 at 17:34

1 Answer 1

2

If you are trying to access multi select element using id the you don't need to set id like Privilege[], you can set any unique identity like privilege-selector but if you are giving name for any multi select element then name must be like Privilege[]

Here is the html :

<form id="form" method="post">
    <select id="privilege-selector" multiple>
       <option value="yahoo">yahoo</option>
       <option value="chrome">chrome</option>
       <option value="mozilla">mozilla</option>
    </select>
    <input type="button" id="Save" Value="SEND"/>
</form>

Please check this below ajax request to post selected data to the server

$("#Save").on("click",function(){
    var selection = [];
    $.each($("#privilege-selector option:selected"),function(index,element){
        selection.push($(element).val());
    })
    $.ajax({
        url : "test5.php",
        type : "POST",
        data : {Privilege:selection},
        success : function(_response){
           var res = JSON.parse(_response);
           if(res.code == "1"){
             console.log(res.data);
           } else {
             alert(res.message);
            }
        }
    })
});

and here is your server file that will handle the incoming request data

$serverResponse = [];
if(isset($_POST['Privilege']) && !empty($_POST['Privilege'])){
    $formattedData = [];
    foreach($_POST['Privilege'] as $key => $value){
        $formattedData[] = array(
            "id" => $key+1,
            "name" => $value
        );
    }
    $serverResponse = ["code"=>"1","message"=>"formatted data","data"=>$formattedData];
} else {
    $serverResponse = ["code"=>"0","message"=>"Please select at least on value"];
}
echo json_encode($serverResponse);
Sign up to request clarification or add additional context in comments.

5 Comments

I changed id="#form" to id="form", the # should not be in there. This was an obvious typo that I edited.
Owe yes # should not be there sorry and thank you @FunkFortyNiner
Obviously thx for the help. But i continue to have the problem. I 've edit the test5.php also with an echo respect the new code suggested , but seems that from test3.php i don't reach test5.php... At the end i can do it also without jquery with the old system html + php...Anyway thx so much for the help!
Ehi man i 've resolved, your code is perfect, the problem was that i 've write http and not hpps to reach the library of Jquery, so Chrome blocked mine request. I've edit on https and now is all ok. THX so much for all!!!
@FabrizioSimi I was still following the question and noticed your comment above. You know, if you want to avoid problems between HTTP and HTTPS in scripts, images, etc. you can use the //script.xx method instead of hard coding them with http:// or https://. That way once online, it will automatically resolve to the current URL the files are in.

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.