如何通过单击标记来更改PHP变量

I just want use ajax to change some variables in PHP code, I search so long time and dont get answers.

there is a a variable which called $args, it's an array,

$arg=array(
    'paged' => $paged,
    'author'=> $author_ID,
    'showposts'=>  $author_posts_num,
    'post_status'=>'publish'
)

and I want to click on a tag, change some variables or add some to $arg,I know PHP is compiles on the server, but what I though is can I change the the variable above before the server compile php code, I mean is this possible: 1:click the tag(maybe a button) 2: change variable above 3: the server compile PHP 4: hte HTML page reload 5: and we see the HTML page has changed

I just new to PHP AJAX, so a little confused here, thank you so much

I just want use ajax … can I change the the variable above before the server compile php code

No. If you are using Ajax, then this is what happens:

  1. Browser requests URL
  2. Server runs PHP and sends the output to the browser
  3. Browser runs JavaScript
  4. JavaScript tells browser to request another URL
  5. Server runs PHP and sends the output to the browser
  6. JavaScript can do something with the result

hte HTML page reload

If you want to reload the page: Don't use Ajax. The entire point of Ajax is to get new content from the server without reloading the page.

Just submit a form to the server and have PHP run and deliver a new page based on the user inform from the form submission.

What you describe is not really a use case of AJAX, if you anyway reload the page, why don't just use a normal hyperlink and provide it with the desired arguments?

AJAX is only used for updating pages (this is a task of JavaScript) without reloading them.

For that purpose you don't need Ajax. The idea behind Ajax is submitting without reloading the page. Not that simple though.

Lets look at in in practice

You can click a button but that button must send the variable you want to show afterwards.

For example you can create a simple form and submit it then assign the value of that form to the variable:

 <?php
 $variable = ( isset ( $_POST[ 'variable' ] ) ) ? 'Your variable is : ' . htmlentities ( $_POST [ 'variable' ] ) : null;

 echo $variable;

 ?>

 <form method="post" action="">
 <input name="variable" />
 <input type="submit" />
 </form>

This is just a very simple form that does what you want

Good luck