PHP post data to user custom url without as an array - php

simply i can use this below code to post any result as an array with message as json format for example:
array('result'=>"{'code':'101', 'message':'ok!!'}")
this format result for my users is not standard and they like to get the result only as an json format for example:
{'code':'101', 'message':'ok!!'}
I can't find other code to instead of my code for return the result
My code:
function redirect_to_customer($result = [], $redirect_url = "")
{
$html = '<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body onload="closethisasap();">
<form name="redirectpost" method="POST" action="' . $redirect_url . '">
';
if (!is_null($result)) {
$result = json_encode($result,true);
$html .= '<input type="hidden" name="result" value="' . str_replace('"', "'", $result) . '"> ';
}
$html .= "
</form>
</body>
<script type=\"text/javascript\">
function closethisasap() {
document.forms[\"redirectpost\"].submit();
}
</script>
</html>";
echo $html;
}

You can post an array in a html form, by using brackets in the field's name:
<form action="" method="post">
<input type="hidden" name="result[]" value="'code':'101'" />
<input type="hidden" name="result[]" value="'message':'ok!!'" />
<input type="submit" value="submit" />
</form>
Will output:
array(1)
["result"]=>
array(2) {
[0]=>
string(12) "'code':'101'"
[1]=>
string(16) "'message':'ok!!'"
}
}

Related

PHP function argument not processed to output

<?php
Function runSearch($name)
{
If(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
This code is suppose to display what is entered into the Search String text box. When I don't use a function it works fine. But as soon as I place the code into the function runSearch there is no output. I'm new to php can an argument be sent to a php function and then displayed on the screen?
you need to call your function, otherwise nothing will happen. Also you need to removed the $name-parameter:
<?php
function runSearch()
{
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
runSearch();
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>

How to create a form that accept number only

How can I create a form that accept number only?
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label>x:</label><br>
<input type="text" name="xx" ><br>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['xx']) || !empty($_POST['xx']) || is_numeric($_POST['xx'])){
$x = $_POST['xx'];
}else{
echo "vous devez taper des chiffres!";
}
echo $x . "<br />";
?>
</body>
</html>
I am new in PHP please an easy answer.
Thank you :-)
If you only want to accept a number, you can also do so using the html element <input>.
Like this:
<input type= "number" name= "xx">
As a precaution, check the server-side $ _POST["xx"] element. Whether the element contains the expected values.
Like this:
check whether the element has been filled in and does not contain only white characters.
And remove any white space before and after the number using trim()
$xx = trim($_POST["xx"]);
if(!empty($xx) && is_numeric($xx)) {
// if "if-condition" true
} else {
// if "if-condition" false
}
I have this message if it is empty
enter image description here
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8"); ?>">
<label>x:</label><br>
<input type="number" name="xx" ><br>
<input type="submit" value="Submit">
</form>
<?php
$value = trim($_POST["xx"]);
if(!empty($value) && is_numeric($value)) {
$xxx = $_POST['xx'];
} else {
echo "vous devez taper des chiffres!";
}
echo $xxx . "<br />";
?>
</body>
</html>
It is not an answer this, I don't know how to add code in the comment.

Trying to pass variable values from PHP to a new HTML file

using PHP 5.6.27 I'm trying to generate a HTML file by combining
(1) Input from form fields
(2) HTML code (template)
Issue: The file gets created but only has the $template value but not the form field input.
Code is below. Also Live link is at http://fastlaw.in/formv2.html
Sure I'm missing something simple, but would appreciate it if anyone can help out.
formv2.html
<form action="formscriptv2.php" method="POST">
<input id="field1" type="text"/> <br />
<input id="field2" type="text" /> <br />
<input type="submit" name="submit" value="Save Data">
</form>
formscriptv2.php
<?php
$Field1 = $_POST['field1'];
$Field2 = $_POST['field2'];
$onepart = "
<div class='table table-left'>
<section>
<h2 class='table-heading'>";
$twopart = "</h2>
<table class='data'>
<caption>
<p>";
$threepart = "</p>
</caption>
";
$template = $onepart . $Field1 . $twopart . $Field2 . $threepart;
$ret = file_put_contents('tmp/mydata.txt', $template, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
?>
PHP $_POST array are populated via the name HTML attribute:
<input id="field1" name="field1" type="text"/> <br />
<input id="field2" name="field2" type="text" /> <br />

PHP post json result to custom url

By default on my web service I must make a post request to custom user's url. I can post the array but I would like to post request as a json payload, the code below could be the post data, but I cannot get the data from PHP.
function redirect_to_customer($result = [], $redirect_url = "")
{
$html = '<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body onload="closethisasap();">
<form name="redirectpost" method="POST" action="' . $redirect_url . '">
';
if (!is_null($result)) {
$result = json_encode($result);
$html .= '<input type="hidden" value="' . str_replace('"', "'", $result) . '"> ';
}
$html .= "
</form>
</body>
<script type=\"text/javascript\">
function closethisasap() {
document.forms[\"redirectpost\"].submit();
}
</script>
</html>";
echo $html;
}
result of this code is :
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
</head>
<body onload = "closethisasap();">
<form name = "redirectpost" method = "POST" action = "http://www.sample.com/response.php">
<input type = "hidden" value = "{'code':'-103','message':'order_id must be unique'}">
</form>
</body>
<script type = "text/javascript">
function closethisasap()
{
document . forms["redirectpost"] . submit();
}
</script>
</html>
response.php user's file content :
<?php
print_r($_POST);
?>
after post data i get empty array
All inputs including <input type = "hidden" need a name, so name = "something".

Form validating in PHP - error message not displayed

I am beginner in PHP and I want to check whether user has filled the input named "jmeno". Name of the corresponding variable is the same. If the input is not entered, then the variable "chybi" should be expanded by text "Zadej jméno!" and that text should appear above the form.
I am getting no errors. If the input is filled, then the form proceeds. If not, then the form doesn't proceed - that works how it's supposed to. But the error message in variable "chybi" doesn't display for some unknown reason in case that the variable "jmeno" is empty (second if).
I have tried many things. It's strange that such a simple script doesn't work. Any ideas? Thank you.
<?php
$chybi = '';
$zacatek = '
<p>some long text</p>
<form action="index.php" method="post" class="akce">
<p>' .$chybi.
'<input type="text" name="jmeno" placeholder="Zadej své jméno," /></p>
<p>
vyber pohlaví<br />
<input type="radio" name="pohlavi" value="žena" /> žena<br />
<input type="radio" name="pohlavi" value="muž" /> muž
</p>
<p>a pokud se nebojíš, <input type="submit" value="vstup!" /></p>
</form>
';
if ( isset($_POST['jmeno']) && isset($_POST['pohlavi']) ) {
$jmeno = $_POST['jmeno'];
$pohlavi = $_POST['pohlavi'];
if ( empty($jmeno) ) {
$chybi .= 'Zadej jméno!<br />';
echo $zacatek;
}
else {
echo "Jmenuješ se $jmeno a jsi $pohlavi.";
}
}
else {
echo $zacatek;
}
?>
As #jylipaa pointed out you're echoing $chybi before setting it's value. Move your logic above the $zacatek varaible.
<?php
$chybi = '';
if ( isset($_POST['jmeno']) && isset($_POST['pohlavi']) ) {
$jmeno = $_POST['jmeno'];
$pohlavi = $_POST['pohlavi'];
if ( empty($jmeno) ) {
$chybi .= 'Zadej jméno!<br />';
}
else {
echo "Jmenuješ se $jmeno a jsi $pohlavi.";
}
}
$zacatek = '
<p>some long text</p>
<form action="index.php" method="post" class="akce">
<p>' .$chybi.
'<input type="text" name="jmeno" placeholder="Zadej své jméno," /></p>
<p>
vyber pohlaví<br />
<input type="radio" name="pohlavi" value="žena" /> žena<br />
<input type="radio" name="pohlavi" value="muž" /> muž
</p>
<p>a pokud se nebojíš, <input type="submit" value="vstup!" /></p>
</form>
';
echo $zacatek;
?>
you are setting $zacatek in the start of code where $chybi is still empty. It is then handled as a string and setting the value of $chybi later on will not change the content of a string afterwards.

Categories