0

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();


    });

    });

Edited by chichi9: forgot to mention button

4
Contributors
13
Replies
75
Views
2 Years
Discussion Span
Last Post by chichi9
0

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

0

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

0

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.

0

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()

0

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

0

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

0

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

Edited by chichi9

0

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

0

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

Edited by chichi9

0

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

Edited by joshuajames.delacruz

0

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

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.