How to pass input filed value using php class and function method - php

I am making admin panel and need to pass a input filed data using php class method like this:
<input type="text" class="form-control" id="title" name="title" onchange="<?php $UpdateObj->SetTitle(); ?>" placeholder="Enter title">
can anyone tell how is it possible. So on change value get to DB.

can anyone tell how is it possible.
It's not.
You can't mix HTML (<input>) or JavaScript (onchange="...") with PHP (<?php ... ?>).
Either use AJAX (which is what you actually want), or use SetTitle() method after submitting the form (which is what you do not want).
Also, learn on basics about HTTP protocol and life of requests, it might give you a hint when is PHP turn, and when is the HTML turn.

Related

How to implement Codeigniter's set_value() function in smarty when repopulating a form?

<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
Works like that in plain HTML, but I've failed to implement in smarty, and I didn't find a good solution.
<input type="text" name="{$field_name}" value="{$value|default:0}" class="form-control">
The line I need to implement to, looks like above.
I didn't manage to solve the problem, so I made a frontend validation also in Jquery.
('#save_form_btn').on('click', function (e) {
if(/*condition*/){
$('#value_id + small').remove();
$('#value_id').addClass('is-invalid');
$('#value_id').after('<small class="text-danger">{"error message"}</small>');
e.preventDefault();
}
});
Note the followings:
I have backend validation also (Codeigniter's form validation)
This code prevents form submit if value is not as expected, so you
don't need to get old data (If user don't pass frontend validation,
backend validation won't be triggered)
This isn't an answer for the original question, but gives a
workaround which can help some maybe.
JQuery is not my cup of coffee, so feel free to improve as needed.

is user defined value equal to server side value?

if i set
<input type="text" id="myinput" value="<?php echo $origValue; ?>">
and use a function with this
$('#myinput').val('i change the value');
will my php variable value be change like
$origValue="i change the value";
and use that variable to different input like
<input type="text" id="secondText" value="<?php echo $origDate; ?>">
and it will show the same value??
if not, how can i do this??
No - if you change the value on the client side, the original PHP value will not be changed. You seem a little confused about the relationship between client and server side.
The PHP is executed first to generate the HTML response. Once this is complete, the HTML is sent to the client, which then allows JavaScript to work on it. The two (JS & PHP) can never directly affect each other without a request being made back to the server.
Therefore, if you change the val() of the input in JS you would need to post that data back to your PHP page, either using a standard form element or an AJAX request.
Is it really that hard to actually 'try it' and find out yourself? It wouldn't take very long..
No it would not change dynamically. The $origValue is already set when the page loads and cannot be updated with jQuery like that. The variable is set when the page loads and cannot be changed.

Have a method for INPUT without using FORM in PHP

I'm being creative and making something new. I am wanting to use <input> without <form> but don't think it's possible to make the input a method (POST) without using <FORM>. I'm currently trying to use
if(isset($_POST['name'])) {
// Do Something
}
but having my input <input type="button" name="name" /> does not activate the isset I have when using PHP to listen for that event.
Is there something I'm doing wrong? Is there another way I should do this? I'm really lost, and don't have no idea what to do now. I do not want to use <form> since I'm currently not interested in using that. Should the input type be "submit"? What else is there I could do?
This is possible with a javascript onclick or onsubmit event, using GET rather than POST, but it's definitely not best practice. Use a form or AJAX, as recommended by other posters:
<input type="text" id="name" />
<button id="submit" onclick="javascript:window.location='http://yoururl.com/?name='+document.getElementById('name').value">Submit</button>
You are not able to send a form data without using <form>.
The only way to do this is to use ajax instead of a classic HTML form.
You can take inputs from STDIN in PHP's latest versions like:
fscanf(STDIN, "%s\n", $value);
Here $value will contain the input.

How to submit form name as object to a php function

I am trying to learn mvc framework in php but then i could not find out a way in passing the form data to a php function in another php page. I am submitting the form and receiving it in the same page and then I am trying to pass the form data to the controller function which will handle the validation. I cannot figure out how should I pass the form data to the other function.
I can pass each data as parameter but then that would be lengthy if there are lots of data in the form. I was wondering if I could pass the form as an object (something like we pass object of structures) to another function and then use the data suitably. I have put in a code module below:
<?php
include('controller.php');
$controller = new Controller($model);
if (isset($_GET['formButton']))
$controller->submitButtonClicked();
?>
<form name="details" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="txt1">First Name:</label>
<input type="text" name="txt1" id="txt1"/>
<label for="password">Password:</label>
<input type="password" name="password" id="password"/>
<input type="submit" name="formButton" id="formButton" value="Submit"/>
</form>
Any help would be very helpful.
If you really want to work with MVC model view controller than i suggest you the codeigniter everything is well defined easy to use visit the official site of codeigniter.
http://ellislab.com/codeigniter.
I hope you will like to work with codeigniter try it and get rid of alot of coding.
The $_GET and $_POST superglobals are already arrays of the submitted form data, so you can simply use these in your controller. Just make the form submit to the controller file directly: this is cleaner and there's no need to pass the $_GET or $_POST (you should probably use post, but I don't know the context).
I assume you're building your own MVC from scratch. If so, you could do a handler.php controller, that every form submits to. This could loop the posted data like so:
// define Input class somewhere and include
$input = new Input();
foreach($_POST as $field => $value)
{
$input->$field = $this->validate($value);
}
In validate() you would do some general validation. Then you could use this new Input object wherever you need the input data. This is a very primitive example of how premade frameworks like CodeIgniter and Laravel use an Input helper class, and of course you can expand on this. Or better yet, save some extra work and utilize a good known framework like those mentioned in your project :)
First of all, why are you using $_GET?
Second, the $_GET is a global, which should be available in any class used by your system (doesn't matter if it's in another file or not).
Otherwise, you can simply just pass them on to the method of the class, if you want to do that for some reason:
...
$controller->submitButtonClicked($_GET);
...

Executing PHP code from DB

This question has been asked multiple times before,
but I have a different situation from those I've read.
My Database multiple forms that are to be loaded upon user request. This is not the problem and I can handle this. Within these forms, there are fields that are filled dynamically from 2 queries.
One of my fields that is to be filled from the database looks like this:
<label class="itemLabel" for="name">Name : </label>
<input name="name" type="text" class="itemInput" value="<? echo $queryB[1]; ?>" readonly="readonly" />
as you can see, the value is set to be a PHP code echo $queryB[1] ... When I got the form from the DB and echoed it, the fields got the value <? echo $queryB[1]; ?> instead of the actual value.
I've tried to use eval($myForm) where my form is retrieved from the DB, but nothing appeared in the place where the form should appear. I would appreciate if someone can help me with this.
Your PHP instance has short_open_tag disabled.
Change it to:
<?php echo $queryB[1]; ?>
...and it should do what you expect.
There is nothing different in your situation and it is exactly the same as others.
and the answer is the same as well: do not mix the code and the data.
Do not store the code in the database.
Do not pass GO, do not collect $200
Implement some sort of placeholders or - even better - some form builder and create these forms on the fly, based on the data from database.
Why not to store only relevant data in the database, like
name
type
value
class
and some flags like disabled, readonly and such?
take a look at http://pear.php.net/package/HTML_QuickForm2/
Building on my comment and Col Shrapnel's answer, here is a simple placeholder example. You should really maintain the HTML in a flat file (as it effectively seems to be part of the view in your application), but for simplicity's sake let's say it still resides in the database. Store the following value:
<label class="itemLabel" for="name">Name : </label>
<input name="name" type="text" class="itemInput" value="{queryValue}" readonly="readonly" />
Now, when you load the value from the database, you can replace the placeholder from the text:
$html = str_replace('{queryValue}', $queryB[1], $htmlTemplate);
This is an incredibly simplified example, and masks a load of potential issues regarding placeholder names, formats etc., but it might get you started.
Alternatively, if you decide to opt for the file route, you could have two files:
view.phtml:
<label class="itemLabel" for="name">Name : </label>
<input name="name" type="text" class="itemInput" value="<?php echo $this->value; ?>" readonly="readonly" />
In your current PHP script:
class View {
public function render($file) {
// check for file existence etc.
require_once $file;
}
}
$view = new View();
$view->value = $queryB[1];
$view->render('view.phtml');

Categories