Can someone explain or show me why my form is only posting in 1 column when it should be posting in 2 columns for example.
Here is my form
<form action="{$baseurl}/redirect" method="post" enctype="multipart/form-data" id="details_change">
<textarea name="about_me" cols="53" rows="5" class="submit_form_textfield">{$profile_user.about_me}</textarea>
<input type="text" name="profile_message" />
<input type="hidden" name="action" value="user_details" />
<input type="submit" class="submit_form_button" value="Update Details" id="details_change">
</form>
And here is my PHP
if (isset($action) && $action=='user_details' && isset($_SESSION['loggeduser_id'])) {
$user = new User();
if (isset($_POST['about_me']) && isset($_POST['about_me'])) {
$_POST['$about_me'] = preg_replace("/[^a-z]/i","",$_POST['about_me']);
} else {
$about_me = '';
}
if (isset($_POST['profile_message']) && isset($_POST['profile_message'])) {
$_POST['$profile_message'] = preg_replace("/[^a-z]/i","",$_POST['profile_message']);
} else {
$profile_message = '';
}
$user->update($_SESSION['loggeduser_id'],array("about_me" => $about_me,"profile_message" => $profile_message));
}
And I have columns in my user table called about_me and profile_message
And what happens is it will only post about_me and not profile_message any reason why?
You need to use $_POST['user_details'] and $_POST['profile_message']
In HTML:
<form action="{$baseurl}/redirect" method="post" enctype="multipart/form-data" id="details_change">
<textarea name="about_me" cols="53" rows="5" class="submit_form_textfield">{$profile_user.about_me}</textarea>
<input type="text" name="profile_message" />
<input type="hidden" name="user_details" value="" />
<input type="submit" class="submit_form_button" value="Update Details" name="action" >
</form>
PHP:
if (isset($_POST['action']) && isset($_SESSION['loggeduser_id'])) {
$user = new User();
if (isset($_POST['profile_message'])) {
$profile_message = preg_replace("/[^a-z]/i","",$_POST['profile_message']);
} else {
$profile_message = '';
}
if (isset($_POST['user_details']) && isset($_POST['user_details'])) {
$user_details = preg_replace("/[^a-z]/i","",$_POST['user_details']);
} else {
$user_details = '';
}
$about_me = $_POST['about_me'];
$user->update($_SESSION['loggeduser_id'],array("about_me" => $about_me,"profile_message" => $profile_message));
}
Related
i made a form where you can put a number and show which one is the highest.
i want to make sure you can put any words in this form.
that there show a notification you cant put words in the form
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="text" name="tweedegetal" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
The following code shows a notification when a letter is typed.
<?php
function maxGetal($getal1, $getal2)
{
if ($getal1 > $getal2) {
return ($getal1);
} elseif ($getal2 > $getal1) {
return ($getal2);
} else {
return ("gelijk");
}
}
?>
<form action="" method="post">
<input type="text" id="eerstegetal" name="eerstegetal" oninput="Eerste_check()" placeholder="Eerste getal"><br>
<input type="text" id="tweedegetal" name="tweedegetal" oninput="Tweede_check()" placeholder="Tweede getal"><br>
<input type="submit" name="submit" value="Bereken hoogste getal">
<p> -----------------------------------------------------------------------</p>
</form>
<?php
if (isset($_POST["submit"])) {
$maxgetal = maxGetal($_POST["eerstegetal"], $_POST["tweedegetal"]);
echo $maxgetal;
}
$input1 = doubleval($_POST["eerstegetal"]);
$input2 = doubleval($_POST["tweedegetal"]);
?>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script>
function Eerste_check(){
var eerstegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(eerstegetal)){
alert('Eerstegetal should contain only numbers.')
}
}
function Tweede_check(){
var tweedegetal = $('#eerstegetal').val()
var pattern = /^\d+\.?\d*$/;
if(!pattern.test(tweedegetal)){
alert('Tweedegetal should contain only numbers.')
}
}
</script>
Try the following:
<input type="number" name="eerstegetal" placeholder="Eerste getal"><br>
<input type="number" name="tweedegetal" placeholder="Tweede getal"><br>
I hope it helps
Like in topic. How can I call the overrider controller from html form like this below:
<form method="post" action="index.php?controller=CmsController">
<input name="test" type="text" />
<input name="action" value="getStatus" type="hidden" />
<input value="test" type="submit" />
</form>
This code above I have in my tpl file.
File: /override/controllers/front/CmsController.php
---edit
{$link->getModuleLink('blockcms', 'CmsController')|escape:'html'}
doesn’t work.
---edit
public function initContent() {
parent::initContent();
if (Tools::getValue('action') && Tools::getValue('action') == 'getStatus') {
$id = $this->getIdByCode(Tools::getValue('voucher'));
$obj = new CartRuleCore($id);
$context = Context::getContext();
$response = $obj->checkValidity($context);
if ($response == NULL or $response == 'Koszyk jest pusty'){
$response = 'Kod ';
}
}
if (isset($this->cms)) {
$this->cms->content = str_replace("<!--response-->", $response, "{$this->cms->content}");
}
$this->setTemplate(_PS_THEME_DIR_ . 'cms.tpl');
This is part of initContent function in \override\controllers\front\CmsController.php And in my file tpl I have
<form method="post" action="index.php?controller=CmsController">
<input name="test" type="text" />
<input name="action" value="getStatus" type="hidden" />
<input value="test" type="submit" />
So I have input fields and when they are submitted, I check to see if their inputted text appears in an array:
<form method="post">
<input type="text" name="1">
<input type="text" name="2">
<input type="text" name="3">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'] {
$array = array('432423', '434', '3', '2', '213');
$success = true;
foreach ($_POST as $key=>$value) {
if (!empty($value) && $key != "submit") {
if (!in_array($value, $array)) {
$success = false;
}
}
}
var_dump($success);
// TRUE if all contains, FLASE if any one doesn't contain
}
?>
But if I wanted a certain input field not to have to be checked against the array, how would I do this
Then you probably want to check for another $key != something?
E.g.
if (!empty($value) && $key != "submit" && $key != "2") {
...
This would exclude the second input...
you can gather all the names in an array just set name="names[]" for the wanted inputs and you will be able to access all the inputs as an array ($_POST['names'])
<form method="post" action="">
<input type="text" name="names[]">
<input type="text" name="names[]">
<input type="text" name="names[]">
<input type="submit" name="submit">
</form>
and loop through them like this
if (! empty($_POST['names']) ) {
$names = $_POST['names'];
foreach ($names as $value) {
if (!empty($value)) {
if (!in_array($value, $array)) {
$success = false;
}
}
}
}
use https://jqueryvalidation.org
<form id="my_form" method="post">
<input type="text" name="a1">
<input type="text" name="a2">
<input type="text" name="a3">
<input type="checkbox" name="chk1" id="chk1">
<input type="submit" name="submit">
</form>
<script type="text/javascript">
$("#my_form").validate({
rules: {
"a1": {
required: true
},
"a2": {
required: true
},
"a3": {
required: true
},
"chk1": {
required: {
depends: function() {
return !$("#chk1").is(":checked");
}
}
}
}
});
</script>
I am trying to insert multiple values in the todo input of the form. but if i do not input a value to any input the null value is inserted in the database. This is my controller action:
public function actionAdd()
{
if (Yii::$app->request->isAjax) {
$request = Yii::$app->request;
$add = new Project();
$add->project_name = $request->post('project');
$add->deadline = $request->post('deadline');
$add->profile_id = $request->post('profile_id');
$add->project_status = "Running";
$add->save();
$getlast = Yii::$app->db->getLastInsertId();
$todo = $request->post('todo');
if (isset($todo)) {
foreach ($todo as $to) {
$add = new Todo();
$add->todo_name=$to;
$add->status="Running";
$add->project_id=$getlast;
$add->save();
}
}
echo json_encode(TRUE); die;
}
echo json_encode(FALSE);die;
}
the form is:
<form class="formclass" method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/todo/add/" role="form" id="register-form" novalidate="novalidate">
<label> Project Name: </label> <input type="name" name="project" class="form-control" placeholder="Project Name" required><br><br>
<input type="hidden" name="profile_id" value="<?php echo $profile_id;?>">
<label> Todo: </label><br>
<textarea type="text" name="todo[]" placeholder="Todo Description..."></textarea>
<div id="dynamicInput">
<span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
</div><br>
<label> Project Deadline:</label><br>
<input type="date" name="deadline"><br><br>
<button type="submit" class="btn btn-default">Add Project</button><BR><BR>
</form>
And the jquery is:
var counter = 1;
var limit = 10;
function addInput(divName)
{
if (counter != limit)
{
var newdiv = document.createElement('div');
newdiv.innerHTML = "<br><textarea type='text' name='todo[]' placeholder='Todo Description...'></textarea>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
Could be you problem is related to the validation rules ..
for check this and only for debugging try use $add->savel(false)
$add = new Todo();
$add->todo_name=$to;
$add->status="Running";
$add->project_id=$getlast;
$add->save(false);
if in this case the rows are inserted the check for validations rule (eventually comment temporary) .. for find the wrong rules or condition
This form takes in data only in the "inp" field. To multiply two numbers you'd have to input the first number then hit "=" to send the number to the "out" field then type in the second number and hit "*". I am attempting to post the results($result) of calculations into the "out" field and not sure how to do this using php. Any help would be greatly appreciated.
php:
$input = $_POST['inp'];
$output= $_POST['out'];
if($_POST['submit'] == 'add') {
$result = $input + $output;
//set result to "out"
}
else if($_POST['submit'] == 'sub') {
$result = $input - $output;
//set result to "out"
}
else if($_POST['submit'] == 'mul') {
$result = $input * $output;
//set result to "out"
}
else if($_POST['submit'] == 'div') {
$result = $input / $output;
//set result to "out"
}
else if($_POST['submit'] == 'equ') {
$result = $input;
//set result to "out"
}
form:
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
If you php and html code in the same file you can write simple write to out input value calculated result. Try something like this:
<body>
<form action = "calc.php" method = "post">
<input type="text"
value="<?=!empty($result)?$result:"0.0"?>" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
I modified your code.
$result= '';
if ($_POST) {
$input = (int) $_POST['inp'];
$output= (int) $_POST['out'];
if(isset($_POST['add']) && !empty($input)) {
$result = $output + $input;
//set result to "out"
}
else if(isset($_POST['sub']) && !empty($input)) {
$result = $output - $input;
//set result to "out"
}
else if(isset($_POST['mul']) && !empty($input)) {
$result = $output * $input;
//set result to "out"
}
else if(isset($_POST['div']) && !empty($input)) {
$result = $output / $input;
//set result to "out"
}
else if(isset($_POST['equ']) && !empty($input)) {
$result = $input;
//set result to "out"
} else {
$error = "Please enter a number";
}
}
<body>
<form action="cal.php" method = "post">
<input type="text" value="<?=!empty($result)?$result:"0.0"?>" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
<p><?=isset($error)?$error:''?></p>