search
HomeBackend DevelopmentPHP Tutorialphp怎么用代码给文本框输入值

有一个文本框,一个按钮,当按了按钮后就先做个判断,如果文本框等于空则(文本框内容等于admin),如果文本框内容不等于则等于输入的内容,这段代码需要怎么写啊

if($_POST['sub']){
//获取文本框的内容
$content=$_POST['content'];
if($content==""){

如果文本框等于空则文本框内容等于"admin"

}

}


回复讨论(解决方案)

<?phpif($_POST['sub']){    //获取文本框的内容    $content=trim($_POST['content']);    if($content==""){        $content = 'admin'    }}

<?phpif($_POST['sub']){    //获取文本框的内容    $content=trim($_POST['content']);    if($content==""){        $content = 'admin'    }}




提示错误:Parse error: syntax error, unexpected '}' in D:\WWW\login.php on line 8


我的代码是:

<?phpif($_POST['sub']){    //获取文本框的内容    $content=trim($_POST['content']);    if($content==""){        $content = 'admin'    }}?>演示

你如果非要用php来判断的话:
$content = (isset( trim($_POST['content'])) && trim($_POST['content']) != '') ? trim($_POST['content']) : 'admin';
?>
或者直接在html用js来设置也是一样的

我也是醉了,php要以分号结尾的,肯定报错。

<?php/** * Created by PhpStorm. * User: Administrator * Date: 12/25/15 * Time: 9:05 AM */if ($_SERVER["REQUEST_METHOD"] == "POST") {    $aval = ($_POST['content']);    if ($aval == "")        $aval = "admin";    echo $aval;}?><html "><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body><h1 id="HELLO">HELLO</h1><form name="" method="post">    <input type="text" name="content"/>    <input type="submit" value="submit" name="idbtn"/></form></body></html>


<?phpif($_POST['sub']){    //获取文本框的内容    $content=trim($_POST['content']);    if($content==""){        $content = 'admin'    }}




提示错误:Parse error: syntax error, unexpected '}' in D:\WWW\login.php on line 8


我的代码是:

<?phpif($_POST['sub']){    //获取文本框的内容    $content=trim($_POST['content']);    if($content==""){        $content = 'admin'    }}?>演示

哈哈,手误,忘了个分号。

<?php/** * Created by PhpStorm. * User: Administrator * Date: 12/25/15 * Time: 9:05 AM */if ($_SERVER["REQUEST_METHOD"] == "POST") {    $aval = ($_POST['content']);    if ($aval == "")        $aval = "admin";    echo $aval;}?><html "><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body><h1 id="HELLO">HELLO</h1><form name="" method="post">    <input type="text" name="content"/>    <input type="submit" value="submit" name="idbtn"/></form></body></html>




你这个代码是将值显示到, echo $aval;  页面上,我想让他显示到这个content文本框内容,

用js实现显示到文本框



<?phpif($_POST['sub']){    //获取文本框的内容    $content=trim($_POST['content']);    if($content==""){        $content = 'admin'    }}




提示错误:Parse error: syntax error, unexpected '}' in D:\WWW\login.php on line 8


我的代码是:

<?phpif($_POST['sub']){    //获取文本框的内容    $content=trim($_POST['content']);    if($content==""){        $content = 'admin'    }}?>演示

哈哈,手误,忘了个分号。
不行了,加了;分好不提示错误了,但是按了之后没有反应,文本框也没有 admin

用js实现显示到文本框

        
JS我知道,我想用php来实现

昨天没有看懂你的意思,今天基本看懂你的意思了。你其实就是想用php来做js应该做的事情,这是多余的。php是服务器端脚本,不能操作dom,你如果非要这么做那得有数据库或者是要设置session,这样做是杀鸡用牛刀。php给文本赋值多在于修改数据用。因为php是通过网页请求来交互数据的,需要刷新页面(当然也可以不刷新页面,那就要用ajax后台无刷新交互了),所以你不用js那用session?用完了还得销毁这个session?何必多此一举?

if($_POST['sub']){
    //获取文本框的内容
    $content=trim($_POST['content']);
    if($content==""){
        $content = 'admin'
    }
}
 
 
?>


演示



"/>


 


这样不就可以了吗?但是这样有什么意义呢?

昨天没有看懂你的意思,今天基本看懂你的意思了。你其实就是想用php来做js应该做的事情,这是多余的。php是服务器端脚本,不能操作dom,你如果非要这么做那得有数据库或者是要设置session,这样做是杀鸡用牛刀。php给文本赋值多在于修改数据用。因为php是通过网页请求来交互数据的,需要刷新页面(当然也可以不刷新页面,那就要用ajax后台无刷新交互了),所以你不用js那用session?用完了还得销毁这个session?何必多此一举?



我有数据库,用session,我就是这个意思呢,管他杀猪杀鸡的,好用就行了。需要怎么做呢

那你就提交的时候取值然后赋值给文本框,根据自己的要求在需要的时候销毁session就行了
if (isset($_POST['content']) && trim($_POST['content'])){
  $content = trim($_POST['content']);
  $_SESSION['content' ] = $content;
}
//这个session在你用完之后销毁,不销毁估计问题也不大,只要不重复名称就可以。
?>


演示



"/>


 


这样应该是可以的,你测试一下,基本思路是这样,如果测试不成功你看下有没有哪个地方少了分号什么的。

那你就提交的时候取值然后赋值给文本框,根据自己的要求在需要的时候销毁session就行了
if (isset($_POST['content']) && trim($_POST['content'])){
  $content = trim($_POST['content']);
  $_SESSION['content' ] = $content;
}
//这个session在你用完之后销毁,不销毁估计问题也不大,只要不重复名称就可以。
?>


演示



"/>


 


这样应该是可以的,你测试一下,基本思路是这样,如果测试不成功你看下有没有哪个地方少了分号什么的。

那你就提交的时候取值然后赋值给文本框,根据自己的要求在需要的时候销毁session就行了
if (isset($_POST['content']) && trim($_POST['content'])){
  $content = trim($_POST['content']);
  $_SESSION['content' ] = $content;
}
//这个session在你用完之后销毁,不销毁估计问题也不大,只要不重复名称就可以。
?>


演示



"/>


 


这样应该是可以的,你测试一下,基本思路是这样,如果测试不成功你看下有没有哪个地方少了分号什么的。

前面php那边有错,你是要如果文本框为空设置为admin对吧?
  $content = trim($_POST['content']);
  if ($content == ''){
    $content = 'admin';
  }
  $_SESSION['content'] = $content;
?>


那你就提交的时候取值然后赋值给文本框,根据自己的要求在需要的时候销毁session就行了
if (isset($_POST['content']) && trim($_POST['content'])){
  $content = trim($_POST['content']);
  $_SESSION['content' ] = $content;
}
//这个session在你用完之后销毁,不销毁估计问题也不大,只要不重复名称就可以。
?>


演示



"/>


 


这样应该是可以的,你测试一下,基本思路是这样,如果测试不成功你看下有没有哪个地方少了分号什么的。

那你就提交的时候取值然后赋值给文本框,根据自己的要求在需要的时候销毁session就行了
if (isset($_POST['content']) && trim($_POST['content'])){
  $content = trim($_POST['content']);
  $_SESSION['content' ] = $content;
}
//这个session在你用完之后销毁,不销毁估计问题也不大,只要不重复名称就可以。
?>


演示



"/>


 


这样应该是可以的,你测试一下,基本思路是这样,如果测试不成功你看下有没有哪个地方少了分号什么的。

前面php那边有错,你是要如果文本框为空设置为admin对吧?
  $content = trim($_POST['content']);
  if ($content == ''){
    $content = 'admin';
  }
  $_SESSION['content'] = $content;
?>
<?php  $content = trim($_POST['content']);  if ($content == ''){    $content = 'admin';  }  $_SESSION['content'] = $content;?><html><head><title>演示</title></head><body><form name="form1" method="post"><input type="input" name="content" value="<?php (isset($_SESSION['content']) && $_SESSION['content']) ? $_SESSION['content']:''; ?>"/><input type="submit" name="sub"  value="提交"/></form> </body></html>

文本框为空的时候点击按钮 ,文本框也没有显示admin啊



那你就提交的时候取值然后赋值给文本框,根据自己的要求在需要的时候销毁session就行了
if (isset($_POST['content']) && trim($_POST['content'])){
  $content = trim($_POST['content']);
  $_SESSION['content' ] = $content;
}
//这个session在你用完之后销毁,不销毁估计问题也不大,只要不重复名称就可以。
?>


演示



"/>


 


这样应该是可以的,你测试一下,基本思路是这样,如果测试不成功你看下有没有哪个地方少了分号什么的。

那你就提交的时候取值然后赋值给文本框,根据自己的要求在需要的时候销毁session就行了
if (isset($_POST['content']) && trim($_POST['content'])){
  $content = trim($_POST['content']);
  $_SESSION['content' ] = $content;
}
//这个session在你用完之后销毁,不销毁估计问题也不大,只要不重复名称就可以。
?>


演示



"/>


 


这样应该是可以的,你测试一下,基本思路是这样,如果测试不成功你看下有没有哪个地方少了分号什么的。

前面php那边有错,你是要如果文本框为空设置为admin对吧?
  $content = trim($_POST['content']);
  if ($content == ''){
    $content = 'admin';
  }
  $_SESSION['content'] = $content;
?>
<?php  $content = trim($_POST['content']);  if ($content == ''){    $content = 'admin';  }  $_SESSION['content'] = $content;?><html><head><title>演示</title></head><body><form name="form1" method="post"><input type="input" name="content" value="<?php (isset($_SESSION['content']) && $_SESSION['content']) ? $_SESSION['content']:''; ?>"/><input type="submit" name="sub"  value="提交"/></form> </body></html>

文本框为空的时候点击按钮 ,文本框也没有显示admin啊

晕啊,大哥,好歹你自己也测试测试嘛,我给你个思路你要去修改一下的,你的那个表单你没发现没有href吗?那提交到哪里去处理呢对不对?下面这段代码无误,我已经测试了,记得表单的href地址改成你自己的。
  if (isset($_POST['content'])){
   $content = trim($_POST['content']);
   if ($content == ''){
      $content = 'admin';
    }
    $_SESSION['content'] = $content;
  }
?>



测试



"/>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)