Home > Mobile >  Web shows blank page after post form submitting
Web shows blank page after post form submitting

Time:11-06

This is my Route. I use resource with TaskController class

Route::resource('main',TaskController::class);

This is my post form in main.blade.php. I know laravel post form must input $crsf

 <form action="" method="post" enctype="multipart/form-data"">
    @csrf
    @method('POST')
  <input type="file" hljs-string">"name="img" accept="image/*">
  <button  name="upload" type="submit" value="ok">Button</button>
  </form>

Here is my TaskController. I use function index() and try to delete process if it shows blankpage agian

 */
public function index(Request $req)
{
    //
    $task = Task::all();
    $data = [
        'nameTH'  =>"",
        'nameEng'   => "",
        'surnameEng' => "",
        'birth' => "",
        'religion' => "",
        'address' => "",
        'regis' => "",
        'expire' => "",
        'serial' => "",
        'task' => $task

    ];
   
    if($req->upload == "ok"){

        $seID = $this->setSecretId("key");
        $seKey = $this->setSecretKey("key");
    $file = "./3.jpg";
    $res = $this->recognizeImage(file_get_contents($file));   
    $data = [
        'nameTH'  => $res["result"]["name_th"],
        'nameEng'   => $res["result"]["first_name_en"],
        'surnameEng' => $res["result"]["last_name_en"],
        'birth' => $res["result"]["date_of_birth"],
        'religion' => $res["result"]["religion"],
        'address' => $res["result"]["address"],
        'regis' => $res["result"]["date_of_issue"],
        'expire' => $res["result"]["date_of_expiry"],
        'serial' => $res["result"]["serial_number"],
        'task' => $task
    
    ];   
   
 
  
}

    return view('main')->with($data);
}

   /**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function show(Task $task)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function edit(Task $task)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, Task $task)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\Models\Task  $task
 * @return \Illuminate\Http\Response
 */
public function destroy(Task $task)
{
    //
}
protected static $_endpoint = 'iai.flashsoftapi.com';
/**
 * $_requestUrl
 * request url
 * @var string
 */
protected static $_requestUri = '/v1/thai-id-card-ocr';

/**
 * $_secretId
 * secret ID
 * @var string
 */
protected $_secretId = '';

/**
 * $_secretKey
 * secret key
 * @var string
 */
protected $_secretKey = '';


/**
 * $_timeOut
 * timeout to connect host
 * @var int in seconds
 */
protected static $_timeOut = 10;

protected static function _signFC1($key, $date, $service, $str2sign)
{
    $dateKey = hash_hmac("SHA256", $date, "FC1".$key, true);
    $serviceKey = hash_hmac("SHA256", $service, $dateKey, true);
    $reqKey = hash_hmac("SHA256", "fc1_request", $serviceKey, true);
    return hash_hmac("SHA256", $str2sign, $reqKey);
}

protected function _sendRequest($url, $payload)
{
    $payload = is_array( $payload ) ? http_build_query( $payload ) : $payload;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, self::$_timeOut);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $timestamp = time();
    $nonce = rand();
    $host = self::$_endpoint;
    $content_type = "application/x-www-form-urlencoded";

    $payloadHash = hash("SHA256", $payload);

    $method = "POST";
    $canonicalUri = self::$_requestUri;
    $canonicalQueryString = "";
    $canonicalHeaders = "content-type:".$content_type."\n".
        "host:".$host."\n";
    $signedHeaders = "content-type;host";
    $canonicalRequest = $method."\n".
        $canonicalUri."\n".
        $canonicalQueryString."\n".
        $canonicalHeaders."\n".
        $signedHeaders."\n".
        $payloadHash;
    $date = gmdate("Y-m-d", $timestamp);
    $service = "th";
    $credentialScope = $date."/".$service."/fc1_request";
    $hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
    $algo = "FC1-HMAC-SHA256";
    $str2sign = $algo."\n".
                $timestamp."\n".
                $credentialScope."\n".
                $hashedCanonicalRequest;
    $signature = self::_signFC1($this->_secretKey, $date, $service, $str2sign);
    $auth = $algo.
            " Credential=". $this->_secretId ."/".$credentialScope.
            ", SignedHeaders=content-type;host, Signature=".$signature;
    $headers = array(
        'X-FC-Timestamp:' . $timestamp,
        'X-FC-Nonce:' . $nonce,
        "Host:" . self::$_endpoint,
        "Content-Type:" . "application/x-www-form-urlencoded",
        "Authorization:" . $auth,
    );

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $resultStr = curl_exec($ch);
    $result = json_decode($resultStr, true);
    if (!$result)
    {
        return $resultStr;
    }
    return $result;
}

/**
 * setSecretId
 * set the secret ID
 * @param string $secretId
 */
public function setSecretId($secretId)
{
    $this->_secretId = $secretId;
    return $this;
}

/**
 * setSecretKey
 * set the secret key
 * @param string $secretKey
 */
public function setSecretKey($secretKey)
{
    $this->_secretKey = $secretKey;
    return $this;
}

public function recognizeImage($image)
{
    $payload = array();
    $payload['image'] = base64_encode($image);
    $url = "https://" . self::$_endpoint . self::$_requestUri;
    return $this->_sendRequest($url, $payload);
}

public function recognizeUrl($url)
{
    $payload = array();
    $payload['url'] = $url;
    $url = "https://" . self::$_endpoint . self::$_requestUri;
    return $this->_sendRequest($url, $payload);
}
//

When I submit my form laravel shows black page. Where may be the problem?

CodePudding user response:

The action attribute in form tag is blank. Change it to the corresponding route

CodePudding user response:

You need to set the action on the form to match your Resource:

   <form action="/main" method="post" enctype="multipart/form-data"">

And after that if you post to that endpoint the controller function that will be invoked is store. Documentation

CodePudding user response:

I have found a solution. I changed the following route:

Route::resource('/main',TaskController::class);

to

Route::get('/main',[TaskController::class,'index']);
Route::post('/main',[TaskController::class,'index']);
  • Related