1

Is it possible in PHP to find and replace a PHP variable with a user defined value from a drop down box on a different PHP page.

Example:

PHP Page 1

$test = '1234'; 

PHP Page 2

Drop Down Values: (Find and replace $test variable with Drop Down selection)

  • 1
  • 2
  • 3
  • 4

Im have not found much information about this.

The purpose is to pass hexadecimal colours based on user choice.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
Try_Fizzle
  • 49
  • 5

2 Answers2

2

PHP Variables are server-side variables. You can not change server side variables from client side directly. Common approaches are: (Although both do same in background)

  1. Using GET to send your data

  2. Using AJAX to dynamically send, fetch and change DOM (Maybe preferred in our case)

On selecting the item on the Drop Down menu, you need to call a method which sends a data to your PHP page and you can change variables.

Your PHP page should handle a GET request change the variable to $test

$test = $_GET["sent_variable"]

While on AJAX, you need to something like:

$.ajax({
    url: "your-php-page.php",
    type: "POST",
    data: { sent_variable: selectedVar}
    }).done(function() {

    //Something here after doing
});

Read more about AJAX here. Note: You have to trigger AJAX on selecting drop-down menu. Read about that here.

Community
  • 1
  • 1
tika
  • 7,135
  • 3
  • 51
  • 82
1

Assuming the dropdown box is part of a form, you can use the 'post' method.

e.g.

<!--HTML-->
<form method="post" action="myScript.PHP">
   <select name="myOption">
    <option value="1">1</option>
    <option value="2">2</option>
   </select>
<input type="submit" value="GO">
</form>   

//myScript.php file
<?PHP
  $test = $_POST['myOption'];
  echo $test;
?>

I actually have a page on my own site that uses similar functionality for passing hex colours if you want to have a look at the HTML source code http://www.wxls.co.uk/formatmyvba.html

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
  • Thanks for the reply that sounds goood but I was looking for a find and replace method to hardcode it. I have a google maps overlay circle (seperate php file to variable) that is dependant on that variable having a defined value as it is the 1st page a user sees. – Try_Fizzle Nov 26 '14 at 18:10