0

Although, this is such a simple question, but I still cannot figure it out. I'm working on a PHP file that is used to get some data from my database and then store it in a jSON string variable $result. I tried to get some value from my select option value element in my HTML using $_POST, and used it in my query. But, unfortunetly, it didn't work. No data stored in my array variable. 'namgi' & 'notrafo' is my select element's name that i will be used in the query.

this is my php code [getdata.php]:

<?php
$server= "localhost";
$user="root";
$pass="";

$namagis=($_POST['namgi']);
$notrafos=($_POST['notrafo']); 

$con = mysql_connect($server,$user,$pass);

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("data", $con);

$query = mysql_query("SELECT * FROM `ukur` WHERE NamaGI=\"$namagis\" && NoTrafo=$notrafos");

$bS = array();
$bS['name'] = 'Beban S';

$bT = array();
$bT['name'] = 'Beban T';

while($r = mysql_fetch_array($query)) {
     $bS['data'][] = $r['BebanS'];   
    $bT['data'][] = $r['BebanT'];       
}

$result = array();

array_push($result,$bS);
array_push($result,$bT);

print json_encode($result, JSON_NUMERIC_CHECK);

mysql_close($con);
?>

The $result variable above, will be used in my Highchart script function. the script

$.getJSON("getdata.php", function(json) {

                options.series[0] = json[0];
                options.series[1] = json[1];
                chart = new Highcharts.Chart(options);
            });

My HTML Code

<html>
<head>
<!-- script here -->

    </head>
    <body>
    <form id="choosegrafik" action="grafconnect.php">
        <select id="namgi">
        <option value="">Choose..</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        </select>
        <select id="notrafo">
        <option value="">Choose..</option>
        <option value="A">a</option>
        <option value="B">b</option>        
        </select>
        <input id="submitgrafik" type="submit" value="Buka Grafik" />
    </form>
    </body>
    </html>

I have searched the solutions in the internet, but still cannot figure it out. A beginners problem :D, So, I hope anyone could help me out. Thanks.,

6
  • You didn't post the html, but given the variable names would be the names of your html input fields, I would assume there is an "s" missing in your POST variables: Maybe try changing $_POST['namgi'] to $_POST['namgis'] and $_POST['notrafo'] to $_POST['notrafos']? However, without seeing the html, this is just a guess. Also, posting the database schema would help, too. Commented Apr 9, 2015 at 1:37
  • Also, I don't know this language, but once it uses namgi, but the variable name is namagis. So there seems to be an "a" missing too. This is exactly the reason why it is recommended to use English name for your variables, tables, etc... What happens if you give your code to someone overseas who doesn't understand your language? Well, it happened right now ;) - and your code is not maintainable for others. Commented Apr 9, 2015 at 1:45
  • @phpPhil thanks for the response :) My html code is just a simple html structure consist of <select name="namgi"><option value="x"> xx </option></select>. What i learnt from php, i can make a new $variable to store a value from my html element using $_POST. So, what i wrote above is not a mistake, its just to differentiate the name. I use this methode in another function and its work so well. I think the problem is because this php function, that used to store some json string,cannt work when i add a $_POST line function, although the syntax is true. Commented Apr 9, 2015 at 7:42
  • @phpPhil to put it simple, i just want to make the query inside my getdata.php file dynamically change based on the option's element value that is selected by user. when the query data result change, my json string data will change too, so then my data that is used in my highchart js change to.. :) and until now, i havent got the solution of this problem. Commented Apr 9, 2015 at 7:49
  • 1
    select elements don't have a name attribute, their id does not get sent to php Commented Apr 9, 2015 at 8:32

1 Answer 1

1
<form id="choosegrafik" action="grafconnect.php" method="POST">

(by default it's sent using GET method that is why you cannot read your vars in the $_POST variable.). And use "name" attribute instead of "id" in your select tags.

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

2 Comments

You beat me to it while I slept :) @anjaryes: You should accept this answer, this is correct.
@phpPhil thanks mister for your advices. And thanks to @_mathieu advice i can solve this problem. :D

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.