This topic is locked
[SOLVED]

Default DropDown value on add page

5/22/2025 6:46:03 PM
PHPRunner General questions
A
asawyer13 authorDevClub member

I want to set a default dropdown id on an add page.

Can't seem to make it happen.

I'm actually grabbing the id from the url but I know it's getting the proper value

I am trying this:

if (isset($_GET['q']) && $_GET['q'] !== '') {
$val = runner_htmlspecialchars($_GET['q']);
$xt->assign("value_WhichReport", $val);
}

Alan

ChatGPT 5/22/2025

Certainly! In PHPRunner, if you want to set a default value for a dropdown field on the add page based on a value you are retrieving from the URL, you can do it using the assign method as you've shown. However, there are a few things to consider to ensure it works properly.

  1. Field Naming: Make sure that "WhichReport" corresponds exactly to the name of your dropdown field in the database and in your PHPRunner project.


  2. Using the Correct Event: Ensure you are placing your code in the correct event. For setting default values, you should place your code in the BeforeProcess or BeforeAdd event of the add page.


  3. Using the Correct Variable: Instead of value_WhichReport, use the variable that corresponds to the dropdown field as defined in PHPRunner. Usually, it’s just the field name without the value_ prefix.



Here’s an updated example:

// This should be in the appropriate event for the Add page
if (isset($_GET['q']) && $_GET['q'] !== '') {
$val = runner_htmlspecialchars($_GET['q']);
$xt->assign("WhichReport", $val); // Use the exact field name
}
  1. Check Dropdown Values: Make sure the value you are assigning ($val) matches one of the values in the dropdown options. If it doesn't match, the dropdown will not display the value you set.


  2. Testing: After implementing the above code, test by navigating to your add page with the appropriate URL parameter (e.g., yourpage.php?q=some_value) and see if the dropdown is pre-populated as expected.



If you follow these steps and ensure that your field names and event placements are correct, the default dropdown value should work as intended.

A
asawyer13 authorDevClub member 5/22/2025

Yes, chatgpt told me the same thing, but it doesn't work.

Alan

lefty 5/22/2025

I think PHPrunner requires defaultValue

if (isset($_GET['q']) && trim($_GET['q']) !== '') {
$val = runner_htmlspecialchars(trim($_GET['q']));
$xt->assign("defaultValue_WhichReport", $val);
}
Notice the V not v.

A
asawyer13 authorDevClub member 5/22/2025

I'm putting the code in the BeforeProcessAdd event.

I get a syntax error with my code and lefty's so I wanted to make sure I was putting it in the right event.

Alan

A
asawyer13 authorDevClub member 5/22/2025
A
asawyer13 authorDevClub member 5/22/2025

If I put the code in the BeforeShowAll event, I don't get an error, but it doesn't do anything.
Alan

Sergey Kornilov admin 5/22/2025

What is "default dropdown id"?

A
asawyer13 authorDevClub member 5/22/2025

My table is
Seq int
Name varchar(50)

in my add page I have a lookup
Seq is the id, and I show name

So in this case Seq = 7 has a name of "Vendor Report" so I would like Vendor Report to be selected when the add page is opened.

Sergey Kornilov admin 5/23/2025

So, you are talking about a "default value" of a field. The best option to set default value is to use ProcessRecordValues event of the Add page:
https://xlinesoft.com/newsletter/linux_version_and_v111_news

Simplified code:
$values["WhichReport"] = $_GET['q']

A
asawyer13 authorDevClub member 5/23/2025

Thanks, I thought I had tried that at one point, but I suspect I put it in the wrong event.
That worked.
Alan