Jump to content

Run query with default option from PHP select query


samadhan

Recommended Posts

I have created select drop-down from MySQL column. When user clicks button it runs php code from another page using get method. I have stored 2 variables in get session. But when first time page is loaded in IE, i get error:

Notice: Undefined index: id  

see attached image.

But if I hit button, query is successful and echoes value in table. There is default option set in dropdown but, again I have to click to query result successfully in IE and Mozilla. No issue on page load in Chrome.
I tried to hit button only once using javascript but it button gets hit repeatedly resulting in page load continuously.

 

 

Main Page:

Hide   Expand arrow-down-16.png   Copy Code
$array_id = $_SESSION['id'];
//print_r($array_id);
 
//Get version id
$version_id = $array_id[array_keys($array_id)[0]];
//echo $version_id;
 
// Find send version number
$print_version = $conn->query("SELECT nx_version FROM workflow1 WHERE id='$version_id';");
$print_version1 = Array();
while($result_step2 = $print_version->fetch_assoc()){
$print_version1[] = $result_step2['nx_version'];
}
 
//echo $print_version1[array_keys($print_version1)[0]];
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' id='frm1' method='get'>";
echo "<html>";
echo "<body>";
    echo "<p></p>";
    echo "<center>";
    echo " Select Base Verison To Compare With : ";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
 
}
echo "</select>";
echo " <button type='submit' id='myButton'>Add Base Verison</button>";    
    echo "</center>";
echo "</body>";
echo "</html>";
    echo "<p></p>";    
    
    $array_select = $_SESSION['data'];
    print_r($array_select);
 
    echo "<form>";

post-204524-0-21673500-1504031574_thumb.jpg

Link to comment
Share on other sites

PHP is telling you there's no such thing as $_SESSION['id']. The 'id' index in $_SESSION is undefined. I don't see anything in the code where you're attempting to set the $_SESSION value, either.

 

$_GET['nx_version'] is going to contain the value of the dropdown after the form is submitted. Again, when the page first loads, though, it's not going to be there. If you reference it, you'll get the "undefined index" error again. If you want to determine whether it's set or not, use isset().

 

I don't really follow what you're doing with the array_keys() thing either...

 

$array_id[array_keys($array_id)[0]]; (???)

 

-John

  • Like 1
Link to comment
Share on other sites

Actually I am trying to get value from database from column step1 and step2 of corresponding nx_version selected. I also want to print nx_version. In another page process.php there is code which is getting selected version and from that I am storing nx_version and its row id in database. Using this on main page I am printing step1, step2 values and also version value.

I think it should be possible to do this stuff on the same main page itself. But don't know how to. Also I want to keep selected version in dropdown.

Please refer this link for another page code:

https://www.codeproject.com/Questions/1202938/Run-query-with-default-option-from-PHP-select-quer

Link to comment
Share on other sites

What would you like to happen when $_SESSION['id'] isn't set? I assume you just want the drop down to perform it's normal behavior, where it just selects the first item in the list. Correct? If so, did you try isset() as suggested earlier?

if(isset($_SESSION['id'])) {
    $array_id = $_SESSION['id'];
} else {
    $array_id = '';
}
 
Also, don't you get an error with the following line:
$version_id = $array_id[array_keys($array_id)[0]];
 
It seems like $array_id would contain a number. If that's the case, I'm not sure what's supposed to happen with array_keys() since the function is designed to get the keys from an array.  :shrug:
 
 
Is PHP set to display all errors and warnings? If you're not sure, try adding the following to the top of your script. Just be sure to remove it when you're done with the debugging process.
error_reporting(E_ALL);
ini_set('display_errors', 1);

 

Link to comment
Share on other sites

the reason you are getting different results in different browsers is because your html markup is completely wrong and different browsers deal with html errors differently.

 

do NOT put a complete html document inside of a form tag.

 

you should be producing a ONE complete and valid html document, with the form located at an appropriate place inside the body of that html document, not the other way around.

Link to comment
Share on other sites

you should be producing a ONE complete and valid html document...

 

@samadhan - you can use W3C's validation service, which is free, to validate your code:

http://validator.w3.org/

 

Note that having a valid HTML document doesn't guarantee the page will look exactly the same in all browsers. It all depends on how the browser developers interpret the specifications for HTML, CSS, etc.

Link to comment
Share on other sites

Thank you all for your comments or suggestions. I got workaround. I displayed dropdown on page where no session data is stored or required. Upon selecting option and clicking button that page, database connection and retrieving data is done and which is stored in previous chart.php page and graph and table is correctly displayed. Just added one more php file. But I had to sacrifice one thing, i.e. default dropdown data, but that was OK.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.