so i am trying to output course information when the course number is selected and the button is clicked.

here is the html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Course Info</title>

</head>
<body>
<h1>Course Information Search</h1>
<form action="" method="post">
<select name="course" id="course">
    <option value="420-121">420-121</option>
    <option value="420-122">420-122</option>
    <option value="420-123">420-123</option>
    <option value="420-221">420-221</option>
    <option value="420-222">420-222</option>
    <option value="420-223">420-223</option>
    <option value="420-224">420-224</option>

<select>
Select a course to see the course name and teacher assigned<br><br>
<input type="button" id="go" value="go!">

</form>
<br><br>
<div id="courseInfo"></div>
</body>
</html>

php

<?php
$filename = "data.txt";
try {
   if ($courses = @fopen($filename, "r")) {
    while (!feof($courses)) {
        $line = fgets($courses);
        $array = explode(", ", $line);

        if (trim($array[0]) == $_GET["course"])
            echo  $array[1] . ", " . $array[2];
    }
    fclose($courses);
   }
   else {
      throw new Exception('file not found.');
   }
}
catch (Exception $e){
   echo "Sorry, ",  $e->getMessage(), "<br>";
}
?>

data file

420-123, Web Interface Design, Jennifer Liutec
420-121, Computer Fundamentals, Amin Ranj Bar
420-122, Introduction to Programming, Samia Ramadan
420-221, Programming with Java, Samia Ramadan
420-224, Configuring and Maintaining Computers, Samia Ramadan
420-222, Web Site Planning and Implementation, Jennifer Liutec
420-223, Operating Systems and Scripting, Soumaya Chaffar

my script(trying)

$(document).ready(function(){

    $("#go").click(function () {

        $('#courseInfo').text();


    });

    });
Member Avatar
diafol

Any chance of telling us what the problem is? What's not working? Where is it failing? What have you tried to fix it?

the problem is nothing is showing up when i clicked the button "go!". it supposed to output the course number, name and teacher from the text file

thanks

Member Avatar
diafol

What are you actually trying to do? The code makes very little sense it's a bit of a car crash (no offence). If you have the data in a file already, I don't see why you don't display the coursename in the select field in the first place.

If you want to get additional info (instructor), without a page refresh, you'll need to use AJAX.

BTW - if you submit with POST - then pick up with POST not GET.

Ofcourse there is nothing shows up, see your js $('#courseInfo').text();, you only set the element text to empty while the field is already empty.
If you want the value of the selected item, please use $('#course').val()

this is the answer

$(document).ready(function(){

    findteacher = function() {

        var file = "course.php?course="+ $("#course").val();
        var file2 = file.split(" ", 2);

        $.ajax({
        type : "GET",
        url : file,
        datatype : "text",
        success : function(response) {


            $("#courseInfo").html("The course: " + response + file2);
        }

    });
}

    clear = function() {
    $("#courseInfo").html("");
};


    $("#course").click(clear);
    $("#go").click(findteacher);
    });

but now im having trouble with the split method, my instructor told me to remove the comma in the text file above with the split method, i tried but its not working

in line 6
var file2 = file.split(" ", 2);

remove the comma and the number it should be like this
var file2 = file.split(" ");

it prints out this The course: Computer Fundamentals, Amin Ranj Bar Taught by: course.php?course=420-121

i wanna get rid of the comma 420-123, Web Interface Design**,** Jennifer Liutec without editing the text file

The Course: Computer Science
Taught by: Teachers Name

So you want the 2nd comma removed ok this code will find the 2nd ,

$s = "420-123, Web Interface Design, Jennifer Liutec";

$res_str = array_chunk(explode(",",$s),2);
foreach( $res_str as &$val){
   $val  = implode(",",$val);
}
echo implode(" ",$res_str);

this will be the output of that
420-123, Web Interface Design Jennifer Liutec

yes that works but it has to come from the file, thats why im kind of stuck

you can try this method using you text file thou it will be different

data.txt file

420-123: Web Interface Design, Jennifer Liutec,
420-121: Computer Fundamentals, Amin Ranj Bar,
420-122: Introduction to Programming, Samia Ramadan,
420-221: Programming with Java, Samia Ramadan,
420-224: Configuring and Maintaining Computers, Samia Ramadan,
420-222: Web Site Planning and Implementation, Jennifer Liutec,
420-223: Operating Systems and Scripting, Soumaya Chaffar,

the code you requested. but im not really sure if this is what you want

$s = file_get_contents($filename);
$res_str = array_chunk(explode(",",$s),2);
foreach( $res_str as &$val){
   $val  = implode(" ",$val);

}
echo implode("<br>",$res_str);

then the output will be like this

420-123: Web Interface Design Jennifer Liutec
420-121: Computer Fundamentals Amin Ranj Bar
420-122: Introduction to Programming Samia Ramadan
420-221: Programming with Java Samia Ramadan
420-224: Configuring and Maintaining Computers Samia Ramadan
420-222: Web Site Planning and Implementation Jennifer Liutec
420-223: Operating Systems and Scripting Soumaya Chaffar

hey thank you so much for trying but the actual code was this

        $.ajax({
        type : "GET",
        url : file,
        datatype : "text",
        success : function(response) {
            var file2 = response.split(" , ");

            $("#courseInfo").html("The course: " + file2[0] + "the teacher: " + file2[1]);

i was printing the wrong string! it was supposed to be my response function not the actual file :)

thank you anyway