-1

我想根据 $_POST 变量更改输入值。像这样的东西:

<?php if ($_POST['yourname'] != "") 
    { 
        $('input[name=yourname]').attr('title', $_POST['yourname']); 
    } ?>

但它不起作用。我怎样才能做到这一点?

谢谢!

编辑:我有这个输入:

<input class="clearme" name="yourname" type="text" title="Insert your name"  />
4

5 回答 5

4

直接在html中添加:

<input class="clearme" name="yourname" type="text" title="Insert your name" value="<?php echo isset($_POST['yourname']) ? $_POST['yourname'] : ''; ?>"  />
于 2012-09-24T09:22:39.963 回答
1

You can't use javascript in PHP. PHP runs on the server, while javascript runs in the clients browser.

You need to do this in pure javascript, not PHP.

于 2012-09-24T09:21:38.787 回答
1

Try this in the one file, replacing 'name_of_cur_file.php' with the name of the current php file.:

<?php

if($_POST['yourname'] > ""){
$yourname = $_POST['yourname'];
}else{
$yourname = ""; // set to avoid errors
}
?>
<form action="name_of_cur_file.php" method="post">
<input type="text" value="<?php echo $yourname;?>" />
<input type="submit" value="go" />
</form>

WARNING!!! - Lacking any validation for this example!!!

于 2012-09-24T09:21:50.677 回答
0

If you want the code sent from the server to execute then:

<?php if ($_POST['yourname'] != "") 
    { 
        <script type="text/javascript">
             $(document).ready(function () {
                 $('input[name=yourname]').attr('title', $_POST['yourname']);
             });
        </script>
    } ?>
于 2012-09-24T09:22:22.293 回答
0

You have jquery code in php.You have complete first php tag then you have write your jquery code in script code like

<?php if ($_POST['yourname'] != "") 
    { 
?>
      <script>
        $('input[name=yourname]').attr('title', $_POST['yourname']); 
      </script>
<?php
    } ?>

You can also write php code only like

 <?php 
$title="Insert your name";
if ($_POST['yourname'] != "") 
        { 

           $title=$_POST['yourname'];

        } ?>

<input class="clearme" name="yourname" type="text" title="<?php echo $title; ?>" />
于 2012-09-24T09:22:22.620 回答