How to pass my Javascript object to php - php

Below I have a function which saves variable on a PHP form to a list of objects in javascript
a.js
function save()
{
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem =
{
'num' : document.getElementById("num").value,
'methv' : document.getElementById("methv").value,
'q1' : document.getElementById("q1").value,
'q2' : document.getElementById("q2").value,
'q3' : document.getElementById("q3").value,
'q4' : document.getElementById("q4").value,
'comm' : document.getElementById("comm").value
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
}
How can i display the contents in my php page so i can edit them? (examples of what it will look like printed out is appreciated)
Edit*
These are the values that will be added to the object
<form action="" method="post" enctype="multipart/form-data">
<select name="methv" class="textfields" id="methv" style="width:110px" >
<option value= "dont know">dont know </option>
<select name="q1" class="textfields" id="q1" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<select name="q2" class="textfields" id="q2" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<select name="q3" class="textfields" id="q3" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<select name="q4" class="textfields" id="q4" style="width:50px" >
<option value= "-">-</option>
<option value= "L">L</option>
<textarea rows="4" cols="40" id="comm" name="comm" style="width:300px"><?php echo $post['addcomment'] ;?></textarea>
</form>
I have only imported a.js and <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

You seem to be confusing the roles of PHP and Javascript.
PHP is server-side, meaning that it renders the page (and does any calculations, database calls, etc, if necessary), and then outputs HTML. This is the end of PHP's involvement in a web page.
Javascript is client-side, meaning after a page has been loaded and rendered in a browser, Javascript can get to work modifying elements of the page, if you'd like.
You haven't included any information about the current page structure, so there is nothing we can offer to help you modify your page content using javascript.

You'll have to submit the data to the server via a GET or POST request in order for a PHP script on the server to work with that data. As long as it's just in JavaScript and localStorage, the server (and, hence, PHP) has no idea it exists.

PSTHow about this ?
When you submit your code you rub the function save.
<form action="" onsubmit="save()" method="post" enctype="multipart/form-data">
And you add to you form tag something like the following
<textarea id="SendToPhp" style="display : none"></textarea>
And in you save function you add this as the end
document.getElementById('SendToPhp').innerHTML = object;
Or something like that !
But i this this is what you are missing!
add the following to the end of your form <input type="submit" value="Send To PHP" /> but keep it inside your <form> tag so place it right before </form>
When you click this button you will be send to the php page that should be in the action ex action = "readjs.php" and in that page do print_r($_POST)
The values you need will be stored in the POST.
If you want to do it without reloading the page then you must look in more into AJAX
Hope this helped in any way.
EDITED CODE, Should be $_POST was previously $_GET

Related

How to get input field value in a variable for a link using PHP

I found this topic:
How to get input field value using PHP
But it didn't help to me. I didn't have any form just this:
echo '<input required type = "number" name = "db" value="1" /><br>';
And I want this input type value store in a variable if I hit a link it should send this checkbox data as well and open the href with these values.
Like ?buy=#product_id&quantity=#insert_value_here
Now it sends only the product_id and the quantity value is empty.
There is a form shown below that will submit to a url and it will add buy=#product_id&quantity=#insert_value_here so you can access those variables in php using. $_GET['buy'] and $_GET['quantity']
This is in the javascript console after submitting using the SO snippet:
You'll need to do some validation to ensure the values are set and that they are the proper type of info. Also, post would be better than get, and using a nonce would help prevent unwanted multiple submissions...
<form action="http://www.google.com" method="get" name="add-to-cart">
<select name="buy">
<option value="1">Product One</option>
<option value="2">Product Two</option>
<option value="3">Product Three</option>
</select>
<input type="number" name="quantity" max="10" min="0" step="1" required />
<input type="submit" value="Submit" />
</form>

Sending value of a select tag to a PHP page as a POST variable

I have a select tag with different values as shown below.
I want to send the selected value to a PHP page as a POST variable or any other way.
Is this possible?
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
Put your select inside a form. Give the form an action that is a php file. give it the method of post and include a <input type="submit" value="Submit"> inside the form as well.
Then, in your php file you will access the $_POST array
echo $_POST['city'];
If you'd like to submit it without the button, you'll need to use javascipt's onChange event.
Yes, and it's very simple. You have to use the from tag : http://www.w3schools.com/html/html_forms.asp and learn a little of PHP and HTML basic interaction
<form action = 'myPage.php' method = 'post'>
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
<input type = 'submit' name = 'send' value = 'send'/>
</form>
(I also added a button son send data)
in myPage.php :
<?php
if(isset($_POST['city'])){
echo isset($_POST['city'];
}
?>

Using <select> and <input type='text'> together

I'm trying to use both a <select> field and <input type='text'> field together with a $_GET method. Basically I'm hoping to have something like this:
<select>
<option name='artist'>Artist</option>
<option name='song'>Song</option>
</select>
<input type='text' name='value' />
And then hopefully for it to redirect as ?artist=value or ?song=value depending on the selected. I hope this makes sense and any help would be appreciated.
No, you cannot do that. Not directly.
You should attach the name attribute to the select element. Then $_GET will contain your_select_name=option_value.
Then just correlate your $_GET['type'] and $_GET['value'] in the backend.
<form method="GET">
<select name="type">
<option value='artist'>Artist</option>
<option value='song'>Song</option>
</select>
<input type='text' name='value' />
</form>
<?php
echo $_GET['type']; // 'artist' or 'song'
echo $_GET['value']; // value of text input
P.S.: If you need to be strict about forming your URL, you can either provide two inputs and rig up a simple JS script that will hide the input not related to your select choice.
Actually, this idea calls for a little elaboration:
<form method="GET">
<select id='type'>
<option name='artist'>Artist</option>
<option selected name='song'>Song</option>
</select>
<input class='inp' id='song' type='text' name='song' />
<input class='inp' style='display:none' id='artist' type='text' name='artist' />
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$('#type').change(function() {
$('.inp').val('').hide().prop('disabled', true); // hide and clear both inputs
$('#'+$('#type').val() ).prop('disabled', false).show(); //show input corresponding to your select
// Note the prop('disabled') calls - you want to disable the unused input so it does not add an empty key to your query string.
}
</script>
I think you should put the name attribute on the <select> tag.
<select name="Category">
<option>Song</option>
<option>Artist</option>
</select>
<input type="text" name="Value"/>
then it should be fairly easy to validate what have been chosen
exemple (PHP):
if($_GET['Category'] == "Song")
{
//Do stuff here with $_GET['Value']
}
else if($_GET['Category'] == "Artist")
{
//Do stuuf here with $_GET['Value']
}

Append form value to URL that already has variables in it

I've got a page showing the results of a MYSQL query written in PHP. The URL has the variables that the user submitted on the previous page as:
www.mydomain.com/search/?var1=xx&var2=xx&var3=xx
When the user is on the results page they need to be able to sort the results. To do this I've got a SELECT form
<form action="/search<?php echo $urlQuery; ?>" name="order "class="formsrch" method="post" >
<label>sort by:</label>
<select class="order" id="order" name="order" onChange="this.form.submit()">
<option value="pricedesc">Price High to Low</option>
<option value="priceasc">Price Low to High</option>
<option value="dist">Distance</option>
</select>
</form>
The variable $urlQuery contains the string to be appended onto the url:
i.e. $urlQuery = "?var1=xx&var2=xx&var3=xx"
The problem is that when the form is submitted the page is reloaded and at the end of the url is ?order=dist.
Is there a way of replacing the question mark with an ampersand so the page will load and the value of order can be retreived?
Or, if anyone has a better way of doing the whole thing I'm definitely open to suggestions.
Thanks
why don't you put them in the form as hidden?
<?php
$extraVars = "";
foreach($_GET AS $key=>$value) {
$extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
}
?>
<form action="/search" name="order "class="formsrch" method="post" >
<?php echo $extraVars;?>
<label>sort by:</label>
<select class="order" id="order" name="order" onChange="this.form.submit()">
<option value="pricedesc">Price High to Low</option>
<option value="priceasc">Price Low to High</option>
<option value="dist">Distance</option>
</select>
</form>
You could make a hidden field for each of the variables you want to transfer so that they get appended too.
Better still, make the method of the form get and then access all the variables with the $_REQUEST global in the backend script.
Output the other variables as <input type="hidden" name="var1" value="xx" /> instead of as the form action, so they'll get taken into your query string.
With this action="/search" and $urlQuery = "?var1=xx&var2=xx&var3=xx"
then to have the value appended on the querysting you should change the form method to "GET".
If you want to keep the form method to "POST" then some javascript would be required to change the action of the form when the form is submitted.

HTML form submit to PHP script

I am making an HTML form. I want the results to appear in the PHP script.
<form action="chk_kw.php" method="post"> <br />
<select> name="website_string"
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij/option>
</select>
<input type="submit" name="website_string" >
</form>
The problem is I cannot get the value passed to the PHP. if I use value:
<INPUT TYPE="submit" name="website_string" value="selected" >
It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?
Try this:
<form method="post" action="check.php">
<select name="website_string">
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij</option>
</select>
<input TYPE="submit" name="submit" />
</form>
Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.
check.php
<?php
echo $_POST['website_string'];
?>
Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.
<form method="POST" action="chk_kw.php">
<select name="website_string">
<option selected="selected"></option>
<option value="abc">abc</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit">
</form>
As your form gets more complex, you
can a quick check at top of your php
script using print_r($_POST);,
it'll show what's being submitted an the respective element name.
To get the submitted value of the element in question do:
$website_string = $_POST['website_string'];
It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].
You will probably want the following HTML:
<select name="website_string">
<option value="" selected="selected"></option>
<option value="abc">ABC</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit" />
With some PHP that looks like this:
<?php
$website_string = $_POST['website_string']; // or $_GET['website_string'];
?>
Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.
For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
Here is what I find works
Set a form name
Use a default select option, for example...
<option value="-1" selected>Please Select</option>
So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.
Try to use HTML5 attributes now they are supported.
This input
<input type="submit">
should be
<input name="Submit" type="submit" value="Submit">
whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.
You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

Categories