0

Hi
I have a website which is ready to launch apart from one of the key request forms which is not working properly and I just can't understand why. I click 'submit' and the form stays on the screen. I get the following message:
Warning: Invalid argument supplied for foreach() in /home/brighttu/public_html/dev/request_tutor.php on line 499
Your request has been sent and you will be contacted by our Coordinator as soon as possible with a Tutor who matches your preferences.

The form does submit though and the record is added to the database:

There is a lot of code and I have no idea where the problem really lies, but here is the foreach() part:

$rqst_subject_1="";
    $rqst_subject_2="";
    $rqst_subject_3="";
    $rqst_subject_4="";
    $rqst_subject_all="";
    foreach ($rqst_subject as $selectedsubject)  //$rqst_level1
    {
        switch($cnt_subject)
        {
            case "1":
                $rqst_subject_1=$selectedsubject;
                $rqst_subject_all.=$selectedsubject."\n";
            break;
            case "2":
                $rqst_subject_2=$selectedsubject;
                $rqst_subject_all.="\t".$selectedsubject."\n";
            break;
            case "3":
                $rqst_subject_3=$selectedsubject;
                $rqst_subject_all.="\t".$selectedsubject."\n";
            break;
            case "4":
                $rqst_subject_4=$selectedsubject;
                $rqst_subject_all.="\t".$selectedsubject."\n";
            break;
        }
        $cnt_subject++;
    }`

The link to the website is: www.dev.bright-tutors.com
The form is: 'Request A Tutor'

I'm really desperate and would be so grateful for any help. Most of the form is in PHP and I couldn't see any sections for Jquery so apologise if I've posted this in the wrong area.

Many thanks

3
Contributors
17
Replies
50
Views
4 Years
Discussion Span
Last Post by ebanbury
0

Hi thanks for getting back to me. The problem is that this part of the page is not my code and I'm not familiar with jquery and arrays,
Would this $rqst_subject need to be amended here in this foreach case section?

Many thanks

0

Hi
It is just coming from the POST:

$rqst_subject = $_POST['rqst_subject'];

I have 2 x foreach()
The first looks like this and is part of a pile of jqery dropdown field and field validation code:

<?php
                $rqst_subject = $_POST['rqst_subject']; 
                $total_cnt_subject = count($rqst_subject);
                $cnt_subject = 1;
                $rqst_subject_all="";
                foreach ($rqst_subject as $selectedsubject)  
                {
                    switch($cnt_subject)
                    {
                        case "1":
                            $rqst_subject_all.=$selectedsubject;
                            $rqst_subject_all.= ($total_cnt_subject > 1 ? "," : "");
                        break;
                        case "2":
                            $rqst_subject_all.=$selectedsubject;
                            $rqst_subject_all.= ($total_cnt_subject > 2 ? "," : "");
                        break;
                        case "3":
                            $rqst_subject_all.=$selectedsubject;
                            $rqst_subject_all.= ($total_cnt_subject > 3 ? "," : "");
                        break;
                        case "4":
                            $rqst_subject_all.=$selectedsubject;
                        break;
                    }
                    $cnt_subject++;
                }
                ?>
                var data = "<?php echo $rqst_subject_all;?>";
                jQuery("#rqst_subject").val(data.split(","));   

            });
        <?php
        }
        ?>

And the second just underneath the list of POSTED items is:

$cnt_subject=1;
    $total_cnt_subject = count($rqst_subject);
    $rqst_subject_1="";
    $rqst_subject_2="";
    $rqst_subject_3="";
    $rqst_subject_4="";
    $rqst_subject_all="";
    foreach ($rqst_subject as $selectedsubject)  //$rqst_level1
    {
        switch($cnt_subject)
        {
            case "1":
                $rqst_subject_1=$selectedsubject;
                $rqst_subject_all.=$selectedsubject."\n";
            break;
            case "2":
                $rqst_subject_2=$selectedsubject;
                $rqst_subject_all.="\t".$selectedsubject."\n";
            break;
            case "3":
                $rqst_subject_3=$selectedsubject;
                $rqst_subject_all.="\t".$selectedsubject."\n";
            break;
            case "4":
                $rqst_subject_4=$selectedsubject;
                $rqst_subject_all.="\t".$selectedsubject."\n";
            break;
        }
        $cnt_subject++;
    }

Other than $rqst_subject being part of the POST

$rqst_subject = $_POST['rqst_subject'];

It isn't listed anywhere else. At least I dont' think so, but as I said I don't really understand the jqery part.

0

There will only be an array in $_POST['rqst_subject'] if there are multiple dropdowns with the same name, e.g.

<select name="rqst_subject[]">
...
</select>

<select name="rqst_subject[]">
...
</select>
0

Thanks. This is why I think it is in the jquery,

jQuery("#rqst_subject").html(listitemsclear);
                jQuery("#rqst_level2").val("<?php echo $_POST['rqst_level2'];?>");   
            });
            jQuery.getJSON("getdropdown.php?task=subjectaccess&level2=<?php echo $_POST['rqst_level1'];?>",  
            function(j)
            {   
                var listitems="";  
                for (var i = 0; i < j.length; i++) 
                {   
                    if(j[i].english!="0")
                    { listitems += '<option value="English">English</option>'; }
                    if(j[i].chinese!="0")
                    { listitems += '<option value="Chinese">Chinese</option>'; }
                    if(j[i].malay!="0")
                    { listitems += '<option value="Malay">Malay</option>'; }
                    if(j[i].tamil!="0")
                    { listitems += '<option value="Tamil">Tamil</option>'; }
                    if(j[i].maths!="0")
                    { listitems += '<option value="Maths">Maths</option>'; }
                    if(j[i].biology!="0")
                    { listitems += '<option value="Biology">Biology</option>'; }
                    if(j[i].chemistry!="0")
                    { listitems += '<option value="Chemistry">Chemistry</option>'; }
                    if(j[i].physics!="0")
                    { listitems += '<option value="Physics">Physics</option>'; }
                    if(j[i].gp!="0")
                    { listitems += '<option value="GP">GP</option>'; }
                    if(j[i].science!="0")
                    { listitems += '<option value="Science">Science</option>'; }
                }
                jQuery("#rqst_subject").html(listitems);
                <?php
                $rqst_subject = $_POST['rqst_subject']; 
                $total_cnt_subject = count($rqst_subject);
                $cnt_subject = 1;
                $rqst_subject_all="";
                foreach ($rqst_subject as $selectedsubject)  
                {
                    switch($cnt_subject)
                    {
                        case "1":
                            $rqst_subject_all.=$selectedsubject;
                            $rqst_subject_all.= ($total_cnt_subject > 1 ? "," : "");
                        break;
                        case "2":
                            $rqst_subject_all.=$selectedsubject;
                            $rqst_subject_all.= ($total_cnt_subject > 2 ? "," : "");
                        break;
                        case "3":
                            $rqst_subject_all.=$selectedsubject;
                            $rqst_subject_all.= ($total_cnt_subject > 3 ? "," : "");
                        break;
                        case "4":
                            $rqst_subject_all.=$selectedsubject;
                        break;
                    }
                    $cnt_subject++;
                }
                ?>
                var data = "<?php echo $rqst_subject_all;?>";
                jQuery("#rqst_subject").val(data.split(","));   

            });
        <?php
        }
        ?>

Unfortunately this page was originally just in PHP (written by me) and a friend who has since disappeared added the jquery for better dropdowns. She has disappeard now and suddenly the form is not working :-(

0

In the form there is only the one rqst_subject field, but you can select multiple subjects. The subjects that show up in the dropdown is dependant on what 'level' has been selected in the previous field.

0

Ah. I see the old multiple selection ploy eh? This looks like a terribly complicated set of snippets for what you're trying to achieve.

You're using Ajax to send data to a DB without page refresh and/or to display some data on the form page fine or are you creating new form controls based on the selected options?

You're requesting json data from somewhere, but I don't see any json_encode() in your php. In fact I can't work out which bits are from the getdropdown.php file.

Correct me if I'm wrong here...

You have a dropdown for all subjects and the user can choose up to 4 subjects. IMO, checkboxes will be easier for the user - multiple select dropdowns can be a pain.

On submission of said dropdown data (Ajax or otherwise), a list of tutors is displayed.

Is there any more to it?

BTW - your link is broken

Edited by diafol

0

Yep you pretty much have it. Yes the user can select up to 4 subjects.
Funny I'm on the site right now link seems fine
Click Here
The strange this is that the form was working, and then suddenly stopped...

0

There is a lot of code in this page and I'm not sure which are really the bits that are going wrong. The original error message at the top, is also supplemented with an 'Error Querying Database' depending on what OS version your on. The validation all works and it also sends out the email messages on submit.
I don't really want to just paste the entire page in here......:-(

0

OK, Now I see the DETAILS OF YOUR REQUEST section

As mentioned, I'd use checkboxes - much less hassle.

<input type="checkbox" class="subject" name="rqst_subject[]" value="3" /><label>Science</label>
<input type="checkbox" class="subject" name="rqst_subject[]" value="7" />
<label>English</label>
<input type="checkbox" class="subject" name="rqst_subject[]" value="2" />
<label>Spanish</label>
<input type="checkbox" class="subject" name="rqst_subject[]" value="1" />
<label>Maths</label>
<button id="sendform">Get Tutors</button>

This way you can limit choices easily with jQuery...

$(".subject").click(function(){
    var countChk = $(".subject:checked").length;
    if(countChk >= 4) 
    {
        $('.subject').not(':checked').attr("disabled", true);
    }else{
        $('.subject').not(':checked').attr("disabled",false);
    }
});

Anyway, the values are passed to the php file for retrieving tutors...

var mySerializedData = ...; // get the relevant fields from form
var request = $.ajax({url: "folder/tutors.php", dataType: "html", type: "GET", data: mySerializedData});
request.done(function( dt ) {
  $("#tutorlist").html( dt );
});
request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});

In the tutors.php page you simply pick up the subjects from the $_GET['subject'] variable.

$getSubjects = (array) $_GET['subject'];
$subjectInts = array_map("intval", $getSubjects);

Then get all tutors based on preferences and subjects from the SQL.

In your 'loop', you may as well build up the html support so you can inject it straight into a container in the receiving page.

Sorry if this is a little off-topic or away from where you're at, but like I said, it looks far too complicated for what it is.

0

I totally hear you. The problem is I don't really have time to change it all and I'm sure the fix is really simple if only I knew where it was.....I've attached a .txt file.

0

I understand your problem. Unfortunately, you'll be looking for a quick fix or patch for something that probably needs an overhaul (before its even released!).

It seems to be working, but a little slow - which is probably due to ajaxing on a slow server. If your data is static and not likely to change, you could write all the data to js variables, which you use to update the dropdown(s) on click/change - so no more round trips to the server on each selection.

0

Hi Many thanks for all the help & advice. In the end I've gone for the quick fix and just updated the relevant line to be an array.

<select name="rqst_subject[]" multiple="multiple" style="width: 120px"  class="roundedfield" id="rqst_subject" size="3">

Thanks Again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.