1

I am not a technical person, but a semi technical business man and learning/creating my own website.

I want to pass JS variables (from html page) to my another Php page.
NOTE** I am getting redirect with required parameters in URL (title). Now, I want to pass another variables, but not through the URL

Here is my html page code:

    $.ajax({
        type: 'GET',
        url: "'search_result.php' + '?title=' + dtitle",
        data: { selectedFT: "hello"}
    });

And my php page code:

<php>
$fueltype = $_GET['selectedFT'];
echo $fueltype;
// I will be using this variable as $fueltype in my further code.
?>

Appreciate your help. I wasted many hours to solves this through google, but nothing fits.
Please provide me sample so that I can understand easily.

5
  • If I understand your question, you want to pass additional variables, without them showing in the address bar of the webpage. Is that correct? Commented Jan 30, 2021 at 7:27
  • If you are getting redirected - i.e. the browser opens a new page - , then this suggests the Ajax code isn't working at all. The whole point of Ajax is that the browser doesn't move to a new page. Open your browser's Developer Tools (press F12 in most browsers), run your code and then check the Console section for errors. (You may need to ensure the Preserve Log option is ticked so it doesn't reset the log when a new page is loaded) Commented Jan 30, 2021 at 8:07
  • @TimothyAlexisVass, yes this is correct. Commented Jan 30, 2021 at 15:18
  • 1
    Then you should use POST method instead of GET. Commented Jan 30, 2021 at 18:42
  • ok, thanks @TimothyAlexisVass Commented Jan 31, 2021 at 5:37

2 Answers 2

1

Post method may help you:

$.ajax({
        type: 'post',
        data: {
            title: dtitle,
            selectedFT: "hello"
        },
        url: 'search_result.php'
    });

In Your PHP page add:

<php>
$fueltype = $_POST['selectedFT'];
echo $fueltype;
// I will be using this variable as $fueltype in my further code.
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this solution.

let dtitle = "Hello";
$.ajax({
    type: 'GET',
    url: `search_result.php?title=${dtitle}`,
});

PHP file

<?php
   $fueltype = $_GET['title'];
   echo $fueltype;
?>

2 Comments

Thanks @tavros but this is not expected. passing title works fine. Which I am passing in URL. I don't want to send variable through URL. Reason is: SEO and Passing variable will create a problem. I have many variables to pass based on several conditions and some operation which I want to perform on next page rather before passing them.
So in that case you need to use the POST method, instead of GET.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.