I'm working on a CakePHP project. I want to merge the values of $value into a string/array so I can later merge it with the string "TESTING" then implode the outcome to a single string without spaces!!!
<?php
$merge="TESTING"; ?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
How can this be done? Thanks in advance.
EG: Please I want to keep the code as is. Meaning no for(var i=0;i++...) loops
Example
Let's say the values of the form are 1 2 3 productnr the outcome should be a string 123productnrTESTING no spaces just a string.
Well i wanted to send that data to a bank gateway to be processed. So it goes like this
1)Bank has sent me a string (The one called TESTING)
2)I print my form data (with the above code)
3) I need to send that data + the string provided into an imploded string to the bank gateway. Hope that clarifies some things :)
If I understand your question correctly, you would need to add a hidden field merge as a last one
The form page (View)
<?php
$merge="TESTING";
?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<input type="hidden" name="merge" value="<?php echo $merge; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
The action page (Action Controller)
If you are using CakePHP, you would need to get POST data from the Cake Request object.
$merge = implode($this->request->params);
echo $merge;
Related
I have a form like so:
<?php if (isset($_POST['artist'])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
On the page in question, this form is shown many times in a foreach loop. However, when I submit any given form, it updates all of the forms, which is not what I want.
How can I append the $artist->ID to $_POST['artist'] so that I get something like:
$_POST['artist_1'] to match the checkbox attributes?
You could pair your foreach that generates the frontend form markup with a foreach that processes the form submission. Something like:
<?php
$regex = '/^artist_([0-9]+)$/'
foreach (array_keys($_POST) as $key) {
if (preg_match($regex,$key,$matches)) {
$artistId = (int)$matches[1];
// do something with $_POST[$key] according to $artistId
}
}
This works for a single field submission or a multiple field submission.
Alternatively, you could do something on the frontend in JS (as #smith suggests in the comments) to ensure the form submission always has the same, well-known keys, populating a hidden form with the current submission. With this approach you would have to add another field to the form that contains the ID.
The solution for this was much simpler than I was able to grasp at first, but basically I just had to do this, the key difference between this and my original question being the first two lines:
<?php $artist_form_id = 'artist_'.$artist->ID;
if (isset($_POST[$artist_form_id])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
So I have a categories table in my database and each category has a certain amount of steps assigned to it. I want to have all of the categories displayed as tabs titled with the category name so I need this from the database. I then have a form inside the tabs to insert the amount of steps needed for that category, but this form is looping... I need it inside the foreach to get the category id but without the form looping... Hope that makes sense..
Here is my code:
<?php foreach($categories as $category){ ?>
<div id="<?php echo $category->category ?>" class="tab">
<form id="maxSteps" method="POST" name="maxSteps" action="<?php $PHP_SELF; ?>" enctype="multipart/form-data">
<label for="maxSteps">Amount of steps in form: </label><input style="width:50px;" id="maxSteps" type="text" name="maxSteps" />
<input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" />
<input type="Submit" value="Go" name="maxStepsSubmit" />
</form>
<table id="amountOfStepsForm">
<?php $maxStepsById = $wpdb->get_results( "SELECT * FROM metal_work_max_steps WHERE cat_id = '$category->cat_id'" ); ?>
<?php foreach($maxStepsById as $maxStep){ ?>
<tr><td id="maxStepsRow<?php echo $maxStep->id; ?>"><?php echo "<p>Amount of steps in form is: <b>".$maxStep->steps."</b>" ?></td><td id="editRow<?php echo $maxStep->id; ?>"><a id="<?php echo $maxStep->id; ?>" class='edit'>Edit</a></td></tr>
<input type="hidden" name="catId" value="<?php echo $category->cat_id; ?>" id="catId<?php echo $maxStep->id; ?>" />
<?php } ?>
</table>
</div>
<?php } ?>
Regards
The best thing you can do is to run only one query and retrieve all your data inside an associative array. Then just loop trough this data structure.
The query can be done using a JOIN statement, in order to retrieve the information regarding the max steps for each category.
1- put the Form tag above the for loop
2- <input type="hidden" name="catId" val... need to have dynamic name: name="catId_<?php echo $category->cat_id;?>"
3- the button Submit label GO, and the colse tag of Form need to be out of the for loop (below)
so all the fields will be in 1 form,
and you will have 1 submit button, if that what you need,
and the field names are not the same, so on submit you can read all of them
these are general comments, i think you need to pay attention to.
I'm kinda rookie at PHP but i'm trying to developing a backoffice with some tables and value-editing options.
By now, i have tables like this:
But i'm kinda bugged with this issue:
The change button is being implemented like this:
for ($i = 0; $i < count($a); $i++) {
?>
<tr>
<td><i onclick="
document.form1.deviceFeatValID.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE_ID] ?>;
document.form1.deviceID.value = <?php echo $a[$i][DEVICE_ID] ?>;
document.form1.deviceClassFeatID.value = <?php echo $a[$i][DEVICE_CLASS_FEATURE_ID] ?>;
document.form1.deviceFeatureVal.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE] ?>;
document.form1.submit();" class="icon-refresh" ></td>
<td><?php echo $a[$i]['DEVICE_FEATURE_VALUE_ID']; ?><td><?php echo $a[$i]['DEVICE_ID']; ?><td><?php echo $a[$i]['DEVICE_CLASS_FEATURE_ID']; ?><td><?php echo $a[$i]['DEVICE_FEATURE_VALUE']; ?>
</tr>
<?php
}
?>
That is, by Javascript, i set the input values of this form, in the same .php:
<form name="form1" method="post" action="deviceFeatureValueFRM.php">
<input type="hidden" name="deviceFeatValID"/>
<input type="hidden" name="deviceID"/>
<input type="hidden" name="deviceClassFeatID"/>
<input type="hidden" name="deviceFeatureVal"/>
<input type="hidden" name="hiddenTypeField"/>
</form>
But...do you consider that this is a good practice? Is there another solution?!
The goal of this code is to detect which line is going about to be edited.
Kind regards,
Sam
Why do this with javascript? Just give the input field a value as such:
<input type="hidden" name="deviceFeatValID" value="<?php echo $a; ?>"/>
Where $a can be any variable you would like.
Well, I would recommend that you had all files separately. So let's say you had one file for PHP. One file for Javascript, and one for HTML. So if you want to use php functions or whatever just need to call them via AJAX.
That way you will have a cleaner code and a bit more secure one.
i have a question about formatting the URL parameters in php
page 1 can have these 2 url
http://somewhere.com/page1.php?foo=1
and
http://somewhere.com/page1.php
now on page 1, when i click a button, i want it to redirect to its self but with an additional parameters in the URL like so
http://somewhere.com/page1.php?foo=1&bar=2
or
http://somewhere.com/page1.php?bar=2
depending on the current url. How can i do this in php?
thanks,
Vidhu
First, check if a certain $_GET parameter is set, using isset.
If it is, echo a certain link. If not, echo a different link.
if( !isset($_GET['bar']) ){
echo 'link';
}
else{
echo 'link';
}
You can do a simple get system, So you will need to check if ?foo=1 is set if it is you can echo its content. If you want to show content for bar=2 check if its set and echo its content. This is just a simple way there more ways though.
The following form will submit to the same url with parameters added.
<form action="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="get">
<?php
// Iterate through our query string and add each key value pair as a hidden input
foreach ($_GET as $key => $value)
{
?>
<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>"/>
<?php
}
?>
<!-- new parameter to add -->
<input type="hidden" name="foo" value="1"/>
<input type="submit"/>
</form>
Simply you can do with $_SERVER['PHP_SELF']
For example
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="get">
<input type="hidden" name="foo" value="1"/>
<input type="submit"/>
</form>
Here the htmlentities() is used to avoid PHP_SELF exploitation as a security precaution
If you wish to avoid hidden input fields you can append the query parameters like action="<?php echo $_SERVER['PHP_SELF']; ?>?foo=1&bar=2"
I'm using these PHP functions to help me build urls correctly:
parse_str()
parse_url()
http_build_query()
http-build-url()
Example:
<?php
function add_parameters($parameters) {
parse_str($_SERVER['QUERY_STRING'], $old_parameters_as_array);
return $_SERVER['PHP_SELF'].'?'.http_build_query(array_merge($old_parameters_as_array, $parameters));
}
echo 'Link';
I want to write a php page that basically takes an array, $_POST['selection'],
lets a user select one of the keys to delete, and refresh itself with the shorter array.
I saw:
echo '< form method="POST" action="Results.php?selection='.urlencode($myArray).'">;
echo '<input type='submit'></form>';
Would this work?
If not, how should I go about this?
You should be able to tailor this to suit your needs, but with such a general question there are tons of answers.
<?php
if (isset($_POST['selection'])) {
$myArray = $_POST['selection']);
} else {
$myArray = array(
'foo',
'bar',
'more',
'jazz',
);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php foreach ($myArray as $key => $value): ?>
<label for="input<?php echo $key; ?>"><?php echo $value; ?></label>
<input id="input<?php echo $key; ?>" type="checkbox" name="selection[]" checked="checked" value="<?php echo $value; ?>" />
<?php endforeach; ?>
<input type="submit" />
</form>
It would not work, urlencode does not accept an array,
it just return a string, Array
If $myArray is an associative array :-
urlencode(http_build_query($myArray));
Function http_build_query will glue the associate key and value like :-
foo=bar&baz=boom&cow=milk&php=hypertext+processor
And the urlencode will encoded the string properly