Testing Symfony 2 form that contains html data - html

Given that a page http://test.intra has the following form code:
<form>
<textarea id="description" name="description"><p>Hello</p></textarea>
<input type="submit" name="submit_save" id='submit_btn' value="Save">
</form>
description field is submitted on browser with value <p>Hello</p>
But using the following code:
$crawler = $client->request('GET', 'http://test.intra');
/* $client is a instance of \Symfony\Bundle\FrameworkBundle\Client ]
* $crawler is a instance of \Symfony\Component\DomCrawler\Crawler
*/
$domForm = $crawler->filter('form');
$domForm = $domForm->selectButton('submit_btn');
$this->client->submit($form);
description field is submited with value &lt;p&gt; &lt;/p&gt;
Should client [\Symfony\Bundle\FrameworkBundle\Client] decode form data before submit is called? Am I missing something?
Thanks

I'm not totally sure what you are asking, but I noticed your code wasn't correct.
$crawler = $client->request('GET', 'http://test.intra');
$domForm = $crawler->filter('form')->form();
$crawler = $this->client->submit($domForm);
Now you'll be able to check your asserts on $crawler.
$this->assertTrue($this->client->getResponse()->isSuccessful());

Related

Passing data from form in html to views.py not working

I passed the id value from html form to views .py. I want to check if the value matches with the one in database. But for some reason it's not working.
list.html
<form method= 'POST' action = "{% url 'jobs:pinned'%}">
{% csrf_token%}
<input type ="text" name = "number">
<input type="submit" value="Submit">
</form>
views.py
def pinned(request,category_slug=None):
users = User.objects.exclude(id=request.user.id)
jobs_list1 = Jobs.objects.all()
if request.method =="POST":
vari = request.GET.get('number')
for evert in jobs_list1:
if evert.Job_Id == vari:
evert.flag = True
evert.save(update_fields=["flag"])
context = {
'job_list1':jobs_list1,
'users':users
}
return render(request, 'jobs/product/list.html',context)
Here, if i put a static value as 511, i.e if evert.Job_Id ==511, it works. But if i change it to request.GET.get('number'), it's not working. How do i send value from form input value to views.py. Thanks.
Firstly method of your form is POST, so for GET method it will work.
for post method try this
vari = request.POST.get('number')
hope it helps
It turns out, I was comparing string with an integer. Thus I solved this problem as:
if request.method =="POST":
vari = request.POST.get('number')
vari = int(vari)
for evert in jobs_list1:
vari1 = evert.Job_Id
.....

MeteorJS: How to get id to load from collection

I'm trying to load an array (with simple text) and trying to load it up on the template whenever it is called. How do I get the ID from that specific item to get the array that I stored in it?
HTML Template:
<template name="commentMarker">
<div id="viewMarker">
<h3 id="markerTitle">{{markerName}}</h3>
<h6 id="markerCategory">{{markerCategory}}</h6>
<br>
<fieldset>
<legend>Description</legend>
<p>{{markerDescription}}</p>
</fieldset>
<form id="commentForm">
<fieldset>
<legend>Comments</legend>
<input type="text" id="markerId" name="idForComment" value={{markerId}}>
<textarea rows="3" cols="19" name="comment" id="commentArea" placeholder="Insert your comment here..."></textarea>
{{#each comments}}
<p id="oneComment">{{this}}</p>
{{/each}}
</fieldset>
<input type="submit" value="Comment" class="commentButton">
<input type="submit" value="Close" class="exitButton">
</form>
</div>
</template>
JS:
Template.commentMarker.helpers({
comments(){
alert(template.find("#markerId").value);
if(commentArray.length===0) return;
else return commentArray;
}});
This is where I insert the comment into the collection's item and it's working fine
Template.commentMarker.events({
'click .commentButton': function(e, template){
e.preventDefault();
var id = template.find("#markerId").value;
var comment = template.find("#commentArea").value;
Points.update(id, { $push: { comments: comment }});
commentArray = Points.findOne(id).comments;
template.find("#commentArea").value = ' ';
}
I tried with commentArray as a global variable which still is. But I'm at loss how I can get the Id from that specific item, I even put it's Id (with hidden display) in the form to actually be able to insert the comment. But it doesn't help me with showing the comments because I cannot seem to get to this field in the Template.helpers ...
Not entirely sure what you are trying to do. It's almost like as if you are displaying the comments right after you updated in to the collection. It looks like you are doing this entirely on local and not a online collection.
However, storing it as a session would work...or reactive var. Might not be the best solution tho. Basically replace commentArray = Points.findOne(id).comments; with:
Session.set('comments', Points.findOne(id).comments)
Then to get it out in helpers:
let commentArray = Session.get('comments')
It's not safe to use it all the time tho for sensitive data. Also try catch the findOne(id).comments because it does produce errors if it happen to not find it.
NOTE: If you are going to use Meteor.Methods, you cannot use Session. You have to return the id and find it in your helpers.

converting PDF to PNG with imagick and HTML input

I am trying to upload a Document (PDF) using HTML , and using imagick to convert it to a PNG if some one can look at my code and help me out I would greatly appreciate it, Thank You!!!
Also when I try to upload my document PDF thru I get a error message
Parse error: syntax error, unexpected '$image' (T_VARIABLE) in
C:\projects\magick2.php on line 27
Line 27 is associated with this code $image->setImageFormat('png')
<!DOCTYPE htlm>
<html>
<head>
<title>TEST!!!</title>
</head>
<div id = "cont">
<form method = "POST" enctype = "multipart/form-data" action = "magick2.php">
<div id = "choose">
File:
<input type="file" name="image" size = "40" id = "fimage" accept = "application/pdf">
<input type="submit" value="UpLoad" name = "wang">
</div>
</form>
</div>
<?php
$image = new Imagick();
$image->readImage(string ($_POST['wang'])
$image->setImageFormat('png');
$image->writeImage('C:\projects\matt.png');
$img = $image;
echo "<img src = matt.png >";
$img->clear();
$img->destroy();
?>
</html>
I am not going in to much details, but the actual reason for that error message is 'missing semi colon on the line above it'.
$image->readImage(string ($_POST['wang'])
you should receive "image" instead of "wang"
$image->readImage(string ($_POST['image'])

Accessing data from Wikidata JSON file

I'm trying to access the following properties from the Wikidata API: id, url, aliases, description and label but so far have been unsuccessful. I'm sure I'm making basic mistakes and so far only have the following code. Any suggestions as to the best way to access this data is much appreciated.
<html>
<body>
<form method="post">
Search: <input type="text" name="q" value="Google"/>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$errors = libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("https://www.wikidata.org/w/api.php?
action=wbsearchentities&search=Google&format=json&language=en");
libxml_clear_errors();
libxml_use_internal_errors($errors);
}
?>
</body>
</html>
Edit - I have managed to get a string ($jsonArr) containing particular data that I would like. However I would like to get the first instance of the particular elements from the string specifically id, url, alias, description and label i.e. specifically: variable1 - Q95, variable2 - //www.wikidata.org/wiki/Q95, variable3 - Google.Inc, varialbe4 - American multinational Internet and technology corporation, variable5 - Google/ Please see code below:
<HTML>
<body>
<form method="post">
Search: <input type="text" name="q" value="Google"/>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$errors = libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("https://www.wikidata.org/w/api.php?
action=wbsearchentities&search=$search&format=json&language=en");
libxml_clear_errors();
libxml_use_internal_errors($errors);
var_dump($doc);
echo "<p>";
$jsonArr = $doc->documentElement->nodeValue;
$jsonArr = (string)$jsonArr;
echo $jsonArr;
}
?>
</body>
</HTML>
You need to show the JSON you want to parse...
Basicly you can get values out of JSON in PHP like this...
If $doc is the JSON you want to parse
$jsonArr = json_decode($doc, true);
$myValue = $jsonArr["keyYouWant"];
Edit after understanding what you are doing there :-)
Hi, Im sorry I was completely confused of what you were doing there... So I have testet your code on my server and just get what you want...
Following the code you wanted... I took clear var names, so they are a little bit longer, but easier to understand...
$searchString = urlencode($_POST['q']); //Encode your Searchstring for url
$resultJSONString = file_get_contents("https://www.wikidata.org/w/api.php?action=wbsearchentities&search=".$searchString."&format=json&language=en"); //Get your Data from wiki
$resultArrayWithHeader = json_decode($resultJSONString, true); //Make an associative Array from respondet JSON
$resultArrayClean = $resultArrayWithHeader["search"]; //Get the search Array and ignore the header part
for ($i = 0; $i < count($resultArrayClean); $i++) { //Loop through the search results
echo("<b>Entry: ".$i."</b>");
echo("<br>");
echo($resultArrayClean[$i]["id"]); //Search results value of key 'id' at position n
echo("<br>");
echo($resultArrayClean[$i]["url"]); //Search results value of key 'url' at position n
echo("<br>");
echo($resultArrayClean[$i]["aliases"]); //Search results value of key 'aliases' at position n
echo("<br>");
echo("<br>");
echo("<br>");
}

Displaying info in form to update - Razor WebMatrix

People, i have been breaking my back searching the internet for the answer to this. I need to know the code needed to show user details in a form, so that they can change and update their info?
I've tried with the following code, but i am hitting a brick wall?
#{
Layout = "~/_template1.cshtml";
var db = Database.Open("StayInFlorida");
var OwnerID = WebSecurity.CurrentUserId;
var FirstName = ("SELECT FirstName from OwnerInfo WHERE OwnerID='OwnerID'");
<h1>My Details</h1>
<form method="post">
<input>#FirstName</input>
<input type="submit" value="Insert" class="submit" />
</form>
}
I'm sure this is really wrong, but help on the net about this is very limited.
Check out the awesome Getting Started tutorials on the ASP.NET Web Pages web site:
http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2/getting-started
The links on the right go to all 9 parts of the article, including extensive details on adding and editing data in a database.
<form action="" enctype="multipart/form-data" method="post">
<input type="submit" value="#FirstName" class="submit" />
</form>
and then to update:
if(IsPost)
{
var FName = Request["FirstName"];
var insertQueryString = "UPDATE OwnerInfo Set FirstName=#0";
db.Execute(insertQueryString, FName);
}
and I don't think you need
<input>#firstName</input>