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

如何通过单击标记来更改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

我只想用ajax来改变PHP代码中的一些变量,我搜索了很长时间并且没有得到答案。

有一个名为 $ args code>的变量,它是一个数组, p>

  $ arg = array(\  n'paged'=> $ paged,
'author'=> $ author_ID,
'showposts'=> $ author_posts_num,
'posst_status'=>'publish'
)
  代码>  pre> 
 
 

我想点击一个标签,更改一些变量或添加一些到 $ arg code>,我知道PHP是在服务器上编译的,但是 我虽然可以在服务器编译php代码之前更改上面的变量, 我的意思是这可能: 1:单击标签(可能是一个按钮) 2:更改上面的变量 3:服务器编译PHP 4 :hte HTML page reload 5:我们看到HTML页面已经改变了 p>

我刚接触PHP AJAX,所以有点困惑,非常感谢你 p> DIV>

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