I have created Wordpress custom fields through functions.php. My code works fine, and I've few input fields and some checkboxes.
Problem is when I save post, even if I don't put content inside my form, these rows are created in DB. I'd like to do some kind of php check and avoid creation of row in DB if field content is not saved.
I tried several ways, but in most cases it would result in incorrect behaviors of checkboxes for example.
Full code is here: http://pastebin.com/embed_js.php?i=Vvnseiep
I'd appreciate your help in this matter. I'm not very experienced.
Thanks!
Here Validation part comes into play. Why cant you use Javascript to validate your input in the client environment itself and then allowing it to hit the db.
Name all of the checkboxes the same(name="meta_box_check[]"). They must still have different ID's. When the form is posted you will be given an array of values that were checked in $_POST['meta_box_check']. You can then check if the array is empty. You can also save the checkbox data as JSON data. This isn't always good practice, but will only use one data row to save any and all checkbox values.
<?php
if(!empty($_POST['meta_box_check']))
{
//process your data and save it
update_post_meta($post_id, 'meta_features_checklist', json_encode($_POST['meta_box_check']));
}
?>
This is a basic example and make sure you do some data validating before saving.
Related
The app I am working on has hidden input variables set on initial load of the page. The variables need to be accessed across all pages. The reason for not converting these to session variables is the user can open another browser with different parameters and passing those value as hidden inputs make it work without any issue. What is the best possible alternative so as to get rid of these hidden inputs..I tried converting all variables into a Structure, but again the structure needs to be posted as part of form submission to make it available to subsequent pages.Another disadvantage of this app is use of frames.I don't have any code to post.
It sounds as if you want different browsers instances which are associated with the same web session to maintain their own distinct sets of data. Doing this by passing form or url variables around seems like a bad idea.
One approach that you could use would be, onSessionStart (or as required), create a structure in the users session to hold instances of the data. For example
session.data = {
someRandomKey: {
valueA: 42,
valueB: "Porridge"
}
}
Then just pass someRandomKey as a hidden form field or querystring parameter. Now, when they submit the form to update variables you use the id from the hidden form field to find the appropriate structure from session.data.
When the user needs a new instance of this form, give them some way, like a link or button, that creates a new unique key, inserts a struct in session.data with this key and populates it with whatever defaults are needed then loads the form passing along the new id as a hidden form field or querystring param again.
I have a HTML table where I am allowing dynamic adding/deleting rows (that contain text-input's in the cells) using some JavaScript. I am not using ASP.NET TextBox controls, just traditional HTML, as I don't think rows containing ASP.NET controls can be added/deleted without a postback.
When the user clicks an ASP:Button, I am using VB to loop through the table rows in server-code, and ultimately use LINQ to write to a database. I have "Imports System.Web.UI.HtmlControls" in the file.
The code where I am getting problems is:
Dim name As HtmlInputText
name = row.Cells(0).Controls(0)
The error is: "Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.HtmlControls.HtmlInputText'." On the 2nd line above.
There is only one input control in each cell, so I am assuming I can use "Controls(0)" to access it. I have read solutions that use "FindControl", but I don't think this works with standard HTML "inputs", but also, as the rows are dynamically added/deleted, it's near-impossible to know the "ID" to search for.
Any ideas? Cheers.
Figured out the problem. Just needed to add runat="server" to the input tags. Thanks every one for the replies.
I have a HTML multiple select being used which caused no issue until a selection with a comma was entered. When saving / loading the values are split by ',' into a list. Therefore causing an issue
I have tried to find a way of possibly changing the character that is being used to split the values when the form is posted but came to a dead end.
Would be very grateful if someone has any insight into this.
Thanks in advance.
---Update with Code---
The control is created dynamically
Dim SelectName1 As New HtmlSelect
SelectName1.ID = "SelectName" & id
SelectName1.Name = "SelectName"
SelectName1.Multiple = True
and filled by looping though the values.
For Each value As String In Request.Form(idToFind).Split(",")
If Not IsDBNull(SelectName.Items.FindByValue(value)) Then....
I cannot add any more code than that, Apologies
After updating the question by add code, I think the available options are limited:
Create an extra Javascript method, that stores the selected values in a json object and store it into a hidden input field. I'm not an expert in asp.net, though I'm sure there exists a method to parse json objects from a string.
Create an extra javascript method, that concatinating all values to a String and store it into a hidden input field. With creation of a single string, you can define your own delimiter.
After that, add this extra Javascript mehod to the event onSubmit. Then submit the form and read the value from hidden input field.
P.S. In my eyes the second idea is more simple to create, than the first one. And maybe there are better ways.
I added two fields to a table, which is used as an embedded form in an admin-generator-module. I created the widgets and changed the templates. I also put the fields in generator.yml, changed schema.yml and executed doctrine:build --all-classes and cc.
Although the fields do appear and the entered data is saved to the MySQL-DB, the data is not shown in the textfields, as the new fields are not part of the MySQL-Query (which goes for every single of the old fields).
I did a grep over the whole application - everything is done exactly the same way as it is for the old fields.
How / where is the MySQL-Query created or cached and how can I (force SF to) change it?
Finally I figured out, what happened...
The query used to be cached by APC. Calling
apc_clear_cache();
apc_clear_cache('user');
apc_clear_cache('opcode');
in preExecute() solved the problem. gosh.....
I have an HTML TABLE that displays my records from mySQL.
I need to edit various records (rows). Is it possible to click on a table row and edit the values in the row inside the HTML table?
I was wondering if there are any PHP scripts out there to guide me?
Yes, it's possible. While I am very convinced PHP is involved, Javascript is involved also.
I think you can have something like this:
1.Click once, javascript increases a counter variable
2. Click twice, javascript checks if var = 2 after increasing, resets counter, and changes innerHTML of the td to something like this:
form action ='yourphpscript.php' method = 'getorpost'>
.....What you want to change....
</form>
3. So then it sends your values to the PHP script, PHP script changes values in database, and you get redirected back to the main page. Or, better yet, use AJAX.
Hope this helped :)